Ios tableView.reloadSections(sectionIndex,带:。无)工作不正常
我创建了3个headerViews并添加了复选框,当选中section0复选框时,我想展开该部分中的单元格。第一次创建所有部分时,当我选中复选框创建单元格时,但当我取消选中复选框时,它不会隐藏/删除单元格。但我想删除所有单元格或隐藏第0节、第1节等中的所有单元格 我的代码是Ios tableView.reloadSections(sectionIndex,带:。无)工作不正常,ios,swift,uitableview,Ios,Swift,Uitableview,我创建了3个headerViews并添加了复选框,当选中section0复选框时,我想展开该部分中的单元格。第一次创建所有部分时,当我选中复选框创建单元格时,但当我取消选中复选框时,它不会隐藏/删除单元格。但我想删除所有单元格或隐藏第0节、第1节等中的所有单元格 我的代码是 var dateArray:[String]?//Globally var button:UIButton?//Globally //Mark - TableView Datasource func tableView(
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
}
}
}
根据我在视图forheaderinsection部分的观察:
复选框按钮在调用重新加载节时再次创建。这就是为什么在onclickDateCheckMark
中,它没有进入if条件if sender.isSelected==true{}
,而是直接进入false条件
创建2个数组,第一个用于数据源,第二个用于存储选定的节索引
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.
在视图加载中
arrDataSource = [arrSection1, arrSection2,arrSection3] // manage your data with single array.. its more dynamic for future purpose
return arrSelectedIndex.contains(section) ? (arrDataSource[section] as AnyObject).count : 0
在numberOfSections中
func numberOfSections(in tableView: UITableView) -> Int {
return arrDataSource.count
}
假设您的viewForHeaderInSection
工作正常,这里给出按钮标签(您已经完成了)
在按钮操作上
// 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)
在第节的行数中
arrDataSource = [arrSection1, arrSection2,arrSection3] // manage your data with single array.. its more dynamic for future purpose
return arrSelectedIndex.contains(section) ? (arrDataSource[section] as AnyObject).count : 0
注意:这是一个想法和最佳方法,如果我忘记了任何条件或您有任何疑问,请在下面发表评论。单击标题,您希望显示该部分的所有单元格??。。。一次是否扩展一个部分??什么是dateArray
?我想展开单元格的一部分。但是,当我选中两个节复选框时,我想扩展两个节单元格。@dahiya_boy,你能问我更详细的情况吗?不介意,但你不会认为数百万个条件给了你一个不必要的麻烦。更好的是,你可以制作额外的数组来存储节索引并检查此数组的扩展折叠。。我更喜欢bcz now代码更方便、更干净。。其余最后的电话将由您决定。。现在,据我所知,您有3个部分,希望在单击时扩展该部分,并在第二次单击时关闭该部分。对于这三个部分,您有三个不同的数组。是的,您是对的,您能为我建议解决方案吗?谢谢,您的代码运行良好,但如何将选中和未选中的图像设置为按钮。button?.setImage(UIImage.init(名为:“取消选中”),用于:UIControl.State.normal)button?.setImage(UIImage.init(名为:“选中”),用于:UIControl.State.selected)@iOS您已经为按钮状态设置了图像,所以在它下面您可以执行button.isSelected=arrSelectedIndex.contains(部分)
非常好…谢谢