Reputation: 17932
I created 3 headerViews and added check box, when select section0 check box i want to expand cells in that section. First time it's creating all sections and when I select check box creating cells, but when i deselect check box it's not hide /removing cells. But i want to remove all cells or hide all cells in section0, section1 etc...
My code is
var dateArray:[String]?//Globally
var button:UIButton?//Globally
//Mark - TableView Datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == filterView?.filterTblView {
if section == 0 {
print(dateArray?.count ?? 0)
return dateArray?.count ?? 0
} else if section == 1 {
return callTypeArray.count
} else {
return departmentArray.count
}
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let height:CGFloat!
if UIDevice.current.userInterfaceIdiom == .pad {
height = 60
} else {
height = 40
}
//Create header view
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: height))
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: headerView.frame.width - 50, height: headerView.frame.height))
lbl.textAlignment = .left
lbl.text = sectionsArray[section]
headerView.addSubview(lbl)
button = UIButton(type: .custom)
button?.frame = CGRect(x: 10, y: 5, width: 30, height: 30)
button?.addTarget(self, action: #selector(self.onclickDateCheckMark), for: .touchUpInside)
button?.setImage(UIImage.init(named: "uncheck"), for: UIControl.State.normal)
button?.setImage(UIImage.init(named: "check"), for: UIControl.State.selected)
headerView.addSubview(button!)
button?.tag = section
return headerView
}
@objc func onclickDateCheckMark(sender:UIButton) {
if sender.tag == 0 {
if sender.isSelected == true {
sender.isSelected = false
DispatchQueue.main.async {
// self.dateArray?.removeAll()
// let indexPaths = self.filterView?.filterTblView.indexPathsForVisibleRows
// print(indexPaths as Any)
// self.filterView?.filterTblView.reloadRows(at: indexPaths!, with: UITableView.RowAnimation.top)
self.dateArray?.removeAll()
let sectionIndex = IndexSet(integer: 0)
self.filterView?.filterTblView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic
}
} else {
DispatchQueue.main.async {
self.dateArray = ["Today", "Yesterday", "This week", "Last week", "This month", "Last month", "Last 30days"]
// print(self.dateArray!)
// let indexPaths = self.filterView?.filterTblView.indexPathsForVisibleRows
// print(indexPaths as Any)
// self.filterView?.filterTblView.reloadRows(at: indexPaths!, with: UITableView.RowAnimation.top)
let sectionIndex = IndexSet(integer: 0)
self.filterView?.filterTblView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic
}
sender.isSelected = true
}
}
if sender.tag == 1 {
if sender.isSelected == true {
sender.isSelected = false
DispatchQueue.main.async {
self.callTypeArray?.removeAll()
let sectionIndex = IndexSet(integer: 1)
self.filterView?.filterTblView.reloadSections(sectionIndex, with: .none)
}
} else {
DispatchQueue.main.async {
self.callTypeArray = ["1", "2", "3", "4"]
let sectionIndex = IndexSet(integer: 1)
self.filterView?.filterTblView.reloadSections(sectionIndex, with: .none)
}
sender.isSelected = true
}
}
if sender.tag == 2 {
if sender.isSelected == true {
sender.isSelected = false
} else {
sender.isSelected = true
}
}
}
As per my observation actually here in viewForHeaderInSection section:
check box button once again creating when i call reloadSections
. Thats why it's not entering into if condition if sender.isSelected == true { }
in onclickDateCheckMark
it's directly entering false condition.
Upvotes: 2
Views: 483
Reputation: 9503
Create 2 arrays, first for datasource and second for to store selected section index.
var arrDataSource : [Any] = [] // your data for all three section, datatype will be as per your requirement
var arrSelectedIndex : [Int] = [] // initially its empty as when VC loads on section will be expanded, if you want to expand any section initially then fill array with that section.
In viewDidLoad,
arrDataSource = [arrSection1, arrSection2,arrSection3] // manage your data with single array.. its more dynamic for future purpose
In numberOfSections ,
func numberOfSections(in tableView: UITableView) -> Int {
return arrDataSource.count
}
Assuming your viewForHeaderInSection
is working properly, here give button tag (you already done)
On buttonAction
,
// First check in section is selected or not.
if arrSelectedIndex.contains(sender.tag){
// IF section is selecetd then remove it from arrSelectedIndex
let aIndex = arrSelectedIndex.firstIndex(of: sender.tag)
if let aInt = aIndex {
arrSelectedIndex.remove(at: aInt)
}
}
else{
// IF section is not selecetd then add it to arrSelectedIndex
arrSelectedIndex.append(sender.tag)
}
// reload tableview
tblView.reloadSections([sender.tag], with: .automatic)
In numberOfRowsInSection,
return arrSelectedIndex.contains(section) ? (arrDataSource[section] as AnyObject).count : 0
Note: This is an idea and best approach, if I forget any condition or you have any doubt then pls comment below..
Upvotes: 1