UITableView意外选择多个单元格

时间:2019-04-23 04:31:51

标签: swift uitableview

我有一个UITableView,每个节加载1个单元格,以提供间隔。当点击一个单元格时,我可以使该单元格适当扩展。问题是其他单元格也会扩展,我无法确定原因。未实现多项选择。 GIF将说明我的问题。我的代码在下面。

Expands cells that are not selected

来自View Controller的代码

EditTextPreference

谢谢!

使用解决方案进行编辑 @Justin的建议是导致问题的原因,单元的状态在重复使用时保持不变。我在自定义单元格中添加了以下内容,问题已解决。

func numberOfSections(in tableView: UITableView) -> Int {
        return searchTextFieldIsEmpty() ? credentials.count : filteredCredentials.count
    }


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


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CredCell
        cell.delegate = self

        if credentials.isEmpty {
            return cell
        } else {
            tableView.backgroundView = nil
            isFiltering() ? cell.formatCell(filteredCredentials[indexPath.section]) : cell.formatCell(credentials[indexPath.section])
            return cell
        }
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CredCell
        cell.detailsView.isHidden = !cell.detailsView.isHidden

        let borderColor = UIColor(red: 206.0/255.0, green: 214.0/255.0, blue: 234.0/255.0, alpha: 1)
        cell.idView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.notificationView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.frontImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.backImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        cell.notesView.addBorder(side: .bottom, thickness: 2, color: borderColor)
        tableView.beginUpdates()
        tableView.endUpdates()
        tableView.deselectRow(at: indexPath, animated: true)
        tableView.scrollToRow(at: indexPath, at: .none, animated: true)
    }

2 个答案:

答案 0 :(得分:0)

您是否要跟踪单元格是展开还是折叠的状态,并在cellForRowAt中使用它。我的猜测是,当您上下滚动时,它会重复使用单元格,有时又会重复使用您展开的单元格,因此其他单元格也会随之扩展。

答案 1 :(得分:0)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CredCell
        cell.tag = indexPath.row
        cell.detailsView.isHidden = !cell.detailsView.isHidden

        if cell.tag == indexPath.row {
            let borderColor = UIColor(red: 206.0/255.0, green: 214.0/255.0, blue: 234.0/255.0, alpha: 1)
            cell.idView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.notificationView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.frontImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.backImageView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            cell.notesView.addBorder(side: .bottom, thickness: 2, color: borderColor)
            tableView.beginUpdates()
            tableView.endUpdates()
        }
        tableView.deselectRow(at: indexPath, animated: true)
        tableView.scrollToRow(at: indexPath, at: .none, animated: true)
    }

尝试使用此代码,可能会对您有所帮助,
祝您编码愉快。...

相关问题