tableView.reloadSections(sectionIndex,with:.none)无法正常工作

问题描述 投票:2回答:1

我创建了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它直接进入虚假状态。

ios swift uitableview
1个回答
1
投票
  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

注意:这是一个想法和最好的方法,如果我忘记任何条件或你有任何疑问,请在下面评论..

© www.soinside.com 2019 - 2024. All rights reserved.