Ios UITableView复选标记仅用于选定行

Ios UITableView复选标记仅用于选定行,ios,swift,uitableview,Ios,Swift,Uitableview,我的问题是 我在应用程序中使用表视图,并希望在每个选定行旁边生成复选标记。但是,除了页面向下滚动的部分中的行之外,其他部分的行中也会生成复选标记。但是,当我打印单击的单元格时,结果是正确的 这是我创建复选标记的代码: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath as IndexPath, animate

我的问题是

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

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

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

}

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

首先在视图控制器中添加索引路径数组:

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())
                }
            }                
        }
    }
}
然后,在您的
cellforrowatinexpath

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

}

请发布您的CellForRowatineXpath方法,因为问题就在这里,而不是在DidSelectRowatineXpathk中,我也在下面的@Miknash中发布了CellForRowatineXpath方法