如何防止同时打开多个表视图单元格

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

我想防止同时打开多个表视图单元格。

这是迄今为止表视图的代码。我可以添加什么来防止这种情况(照片是我当前所在的位置)

extension HelpVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ExpandableTableViewCell.reuseIdentifier, for: indexPath) as? ExpandableTableViewCell else { return UITableViewCell() }
        cell.set(dataSource[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }

}

extension HelpVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dataSource[indexPath.row].isExpanded.toggle()
        questionsTableView.reloadRows(at: [indexPath], with: .automatic)
        
    }
}

这是图像中的问题: img

我尝试使用

tableview.deselectRows
但我无法使其工作。有什么想法吗?

ios swift xcode uitableview tableview
1个回答
0
投票

我明白你的意思,我已经简化了实现。希望它应该是可以理解的。首先,我在 tableView 单元格中使用两个标签,即问题和答案,基本上是一个常见问题解答单元格。因此,我使用了一个 Int 类型的变量,并给出了值 -1,因为否则正数或 0 可能是索引,当我选择一行时,该值保存在 selectedIndex 中,并且表重新加载,之后 Indexpath 处的行单元格进行检查对于索引,不要将答案放入索引的答案标签中,否则索引应变为空。希望对你有帮助。

//MARK: Variable
    var selectedIndex = -1

    extension HelpVC: UITableViewDataSource, UITableViewDelegate {

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "QACell",for: indexPath)as! QATableViewCell
                let question = self.arr1[indexPath.row]["question"]as? String
                cell.question.text = question ?? ""
                if selectedIndex == indexPath.row{
                    let answer = self.arr1[indexPath.row]["answer"]as? String
                    cell.answer.text = answer ?? ""
                }else{
                    cell.answer.text = ""
                }
                return cell
            }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                self.selectedIndex = indexPath.row
                tableView.reloadData()  
        }
      }
© www.soinside.com 2019 - 2024. All rights reserved.