gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:27:30 25 4
gpt4 key购买 nike

我遇到的问题是;

我在应用程序中使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com