我创建了3个headerViews并添加了复选框,当选中section0复选框时,我想在该部分中展开单元格。第一次创建所有部分时,当我选中复选框创建单元格时,但是当我取消选中复选框时,它不会隐藏/删除单元格。但是我想删除所有单元格或隐藏section0,section1等中的所有单元格...
我的代码是
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
}
}
}
根据我在viewForHeaderInSection section:
中的观察结果,复选框按钮在我致电reloadSections
时再次创建。因此,如果if sender.isSelected == true { }
中的条件onclickDateCheckMark
直接进入错误条件,它就不会进入。
答案 0 :(得分:1)
创建2个数组,第一个用于数据源,第二个用于存储所选节索引。
function aaaaa() {
blah = bla blah blah;
}
function bbbbb() {
blah = bla blah blah;
}
function ccccc() {
blah = bla blah blah;
}
function zzzzz () {
aaaaa();
ccccc();
}
在 viewDidLoad 中,
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.
在 numberOfSections 中,
arrDataSource = [arrSection1, arrSection2,arrSection3] // manage your data with single array.. its more dynamic for future purpose
假设您的func numberOfSections(in tableView: UITableView) -> Int {
return arrDataSource.count
}
工作正常,请在此处提供按钮标签(您已经完成)
在viewForHeaderInSection
上,
buttonAction
在 numberOfRowsInSection 中,
// 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)
注意:这是一种想法和最佳方法,如果我忘记了任何条件或有任何疑问,请在下面发表评论。