ios - UITableView 仅选中行的复选标记

标签 ios swift uitableview

我遇到的问题是;

我在应用程序中使用 TableView ,并且希望在每个选定行旁边生成复选标记。但除了页面向下滚动的部分中的行之外,其他部分的行中也会生成复选标记。但是,当我打印单击的单元格时,结果是正确的。

这是我创建复选标记的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)

    let row = indexPath.row
    let indexpath = NSIndexPath(row: indexPath.row, section: indexPath.section)

    let currentCell = tableView.cellForRow(at: indexpath as IndexPath) as! UITableViewCell
    currentCell.backgroundColor = UIColor.gray 

    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark             

    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if (sympton.name == self.symptonName ){
                    self.symptonList.append(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                    print("*******")
                    print(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                }
            }                
        }
    }
}

我的 cellForRowAt 方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath as IndexPath)
    let row = indexPath.row
    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if(sympton.name == symptonName) {
                    cell.textLabel?.text = sympton.questionList[indexPath.section].answerList[row]
                }
            }
        }
    }
    return cell

}

如果您有任何建议,这对我非常有用。

最佳答案

首先在 View Controller 中添加 IndexPaths 数组:

var selectedIndexes: [IndexPath] = [IndexPath]()

然后你的 didSelectRowAtIndexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    if let index = selectedIndexes.index(where: {$0.row == indexPath.row && $0.section == indexPath.section}){
        selectedIndexes.remove(at: index)
    }
    else {
        self.selectedIndexes.append(indexPath)
    }

    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if (sympton.name == self.symptonName ){
                    self.symptonList.append(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                    print("*******")
                    print(sympton.questionList[indexPath.section].question  + " " + sympton.questionList[indexPath.section].answerList[row].lowercased())
                }
            }                
        }
    }
}

然后,在您的 cellForRowAtIndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath as IndexPath)
    let row = indexPath.row
    for organ in self.dataSource.organs {
        if(organ.name == organName) {
            for sympton in organ.symptonList {
                if(sympton.name == symptonName) {
                    cell.textLabel?.text = sympton.questionList[indexPath.section].answerList[row]
                }
            }
        }
    }
    if(self.selectedIndexes.contains(where: {$0.row == indexPath.row && $0.section == indexPath.section})) {
        cell.backgroundColor = UIColor.gray 
        cell.accessoryType = .checkmark             
    }
    else {
         cell.backgroundColor = UIColor.white
         cell.accessoryType = .none             
    }
    return cell

}

关于ios - UITableView 仅选中行的复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50573964/

相关文章:

iphone - 如何检测单元格被选中但未在 UITableViewController 中单击

ios - 如何 swift 获得物体的确切点?

swift 中的 DatePicker 问题

ios - 如何知道用户是否触摸了iAd?

ios - 从不同的 TableView 单元格“segue”到同一个 View Controller

arrays - Swift - setValuesForKeys(dictionary) 错误 - 此类与键的键值编码不兼容

ios - 在 NSURLSession POST 请求后快速重新加载数据的时间是多少

ios - 在索引处重新加载时 TableView 崩溃

ios - 如何显示 UITableViewCell 的自定义 UIMenuItem?

ios - Fat XIB 的内存问题