我在我的应用程序中实现了一个带有部分和复选标记的 tableView。当我点击一个单元格时,我遇到了一个问题,复选标记出现在单元格上,但在 12 行之后重复。
我认为问题出在我的部分,“didSelectRowAtIndexPath”函数使用“indexPath.row”来识别单元格,但是就像我有一些部分一样,我还需要指定“IndexPath.section”来确定哪个部分的哪个单元格轻拍。
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Je compte le nombre de ligne dans tableArray et créer autant de cellule que de ligne
return objectsArray[section].sectionObjects.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectsArray.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//On affiche le boutton pour sauvegarder les catégories
SaveCategorie.hidden = false
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
//Si la cellule est déja cochée
if cell.accessoryType == .Checkmark
{
//je la décoche
cell.accessoryType = .None
}
else {
cell.accessoryType = .Checkmark
}
}
}
尝试存储该项目:
var selectedRowNumber: NSMutableIndexSet!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
cell.accessoryType = .None
if let selectedRowNumber = self.selectedRowNumber {
if indexPath.row == selectedRowNumber {
cell.accessoryType = .Checkmark
}
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//On affiche le boutton pour sauvegarder les catégories
SaveCategorie.hidden = false
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
//Si la cellule est déja cochée
cell.accessoryType = .Checkmark
self.selectedRowNumber.addIndex(indexPath.row)
dump(CatChoosen)
dump(selectedRowNumber)
}
}
但我得到:
致命错误:在展开可选值时意外发现 nil