gpt4 book ai didi

ios - tableView.reloadSections(sectionIndex,: . 无)无法正常工作

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:14 25 4
gpt4 key购买 nike

I created 3 headerViews and added check box, when select section0 check box i want to expand cells in that section.第一次创建所有部分时,当我选中创建单元格的复选框时,但当我取消选中复选框时,它不会隐藏/删除单元格。但我想删除所有单元格或隐藏第 0 节、第 1 节等中的所有单元格...

我的代码是

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 部分的实际观察: 复选框按钮 当我调用 reloadSections 时再次创建。这就是为什么它没有进入 if 条件 if sender.isSelected == true { }onclickDateCheckMark 它直接进入 false 条件。

最佳答案

  1. 创建 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.
  2. viewDidLoad中,

    arrDataSource = [arrSection1, arrSection2,arrSection3] // manage your data with single array.. its more dynamic for future purpose 
  3. numberOfSections中,

    func numberOfSections(in tableView: UITableView) -> Int {
    return arrDataSource.count
    }
  4. 假设您的 viewForHeaderInSection 工作正常,这里给出按钮标签(您已经完成)

  5. 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)
  6. numberOfRowsInSection中,

    return arrSelectedIndex.contains(section) ? (arrDataSource[section] as AnyObject).count : 0

注意:这是一个想法和最佳方法,如果我忘记了任何条件或您有任何疑问,请在下面发表评论。

关于ios - tableView.reloadSections(sectionIndex,: . 无)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55567918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com