我遇到的问题是;
我在应用程序中使用表视图,我希望在每个选定行旁边生成复选标记。但是,除了页面向下滚动的部分中的行之外,还会在其他部分的行中生成复选标记。但是,当我打印单击的单元格时,结果是正确的。
这是我创建复选标记的代码:
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
}
如果您有任何建议,这对我非常有用。
答案 0 :(得分:0)
首先在视图控制器中添加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
}