【问题标题】:When the checkboxes in uitableviewcell are clicked, other cells are affected当 uitableviewcell 中的复选框被点击时,其他单元格受到影响
【发布时间】:2021-12-06 15:02:02
【问题描述】:

我正在尝试制作过滤器。我有一个表格视图。当我单击任何部分时,它会展开和折叠。

我的问题是,当我单击复选框后打开和关闭其他部分时,其他部分中未选中的复选框显示为选中,而选中的复选框未选中。我该怎么办?你能给我看一些代码吗?谢谢!

https://ibb.co/0htP7Hz // 过滤图片

var hiddenSections = Set<Int>()

var filtersArray = Set<String>()        

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "FilterCell", for: indexPath) as? FilterTableViewCell else
    {
        fatalError("Product Group Cell not found")
    }
    
    guard let item = self.filterElementListVM.itemfilterviewmodelAtIndex(indexPath) else {
        
        return UITableViewCell()
    }
    cell.setupCell(title: item.definition ?? "", buttonTag: item.id ?? 0, filterArray: self.filtersArray)
    
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if let cell = tableView.cellForRow(at: indexPath) as? FilterTableViewCell {
        let selectedFilterItem = self.filterElementListVM.itemfilterviewmodelAtIndex(indexPath)
        if cell.buttonCheck.isSelected {
            self.filtersArray.remove(String(selectedFilterItem?.definition ?? ""))
        } else {
            self.filtersArray.insert(String(selectedFilterItem?.definition ?? ""))
        }
        cell.buttonCheckTap()
    }
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let viewHeader = UIView.init(frame: CGRect.init(x: 0.0, y: 0.0, width: tableView.frame.size.width, height: 67.0))
    viewHeader.backgroundColor = .white
    
    let filterVM : FilterViewModel = self.filterElementListVM.filterViewModelAtIndex(section)
    
    let viewFilterHeader : ViewFilter = ViewFilter.init(title: filterVM.definition,
                                                        rightImage: UIImage.init(named: "arrow_down")!, isPropertiesChanged: false, isArrowHidden: false)
    viewFilterHeader.tag = section
    let tap = UITapGestureRecognizer(target: self, action: #selector(hideSection(_:)))
    viewFilterHeader.addGestureRecognizer(tap)
    
    viewHeader.addSubview(viewFilterHeader)
    viewFilterHeader.snp.makeConstraints { (make) in
        make.top.equalTo(7.0)
        make.bottom.equalTo(0.0)
        make.leading.trailing.equalTo(0)
    }
    
    return viewHeader
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 67.0
}

@objc private func hideSection(_ sender: UITapGestureRecognizer? = nil) {
    guard let section = sender?.view?.tag else { return }

    func indexPathsForSection() -> [IndexPath] {
        var indexPaths = [IndexPath]()
        
        for row in 0..<self.filterElementListVM.numberOfRowsInSection(section) {
            indexPaths.append(IndexPath(row: row,
                                        section: section))
        }
        return indexPaths
    }
    
    if self.hiddenSections.contains(section) {
        self.hiddenSections.remove(section)
        self.tableviewFilter.insertRows(at: indexPathsForSection(),
                                  with: .fade)
    } else {
        self.hiddenSections.insert(section)
        self.tableviewFilter.deleteRows(at: indexPathsForSection(),
                                  with: .fade)
    }
}

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    看起来您的配置有问题,并且当您切换您的单元格时,旧数据应用于您的单元格。因此,您需要在 UIColelctionViewCell 类中的 prepareFroReuse() 方法中清除所有数据

    更多信息:https://developer.apple.com/documentation/uikit/uitableviewcell/1623223-prepareforreuse

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2021-06-30
      • 1970-01-01
      相关资源
      最近更新 更多