滚动时,UITableView复选标记消失

我必须做一个tableView的复选标记,但如果我滚动和一个检查标记单元格是不可见的,我滚回复选标记消失。

运行此代码时

var boolArray = [Bool]() func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! if cell.accessoryType == UITableViewCellAccessoryType.Checkmark { cell.accessoryType = UITableViewCellAccessoryType.None boolArray[indexPath.row] = false } else { cell.accessoryType = UITableViewCellAccessoryType.Checkmark boolArray[indexPath.row] = true } println(boolArray) } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { boolArray.append(false) var view = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CellTable") return view } 

经过一点滚动和复选标记,印刷arrays是这么大…

[真,假,真,真,假,假,假,假,假,假,假,假,假,假,假,假,假,

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ var cell : UITableViewCell = ......... if(boolArray[indexPath.row){ cell.accessoryType = UITableViewCellAccessoryType.Checkmark } else { cell.accessoryType = UITableViewCellAccessoryType.None } } 

试试这个代码。

创build一个元组数组来存储行和值的值:

 var selectedCells:[(row: Int, section: Int)] = [] 

在你的cellForRowAtIndexPath ,检查你是否有任何selectedCellValues 。 如果是这样,为该单元添加一个accessoryType并跳出循环,

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell = tableView.dequeueReusableCellWithIdentifier("SettingsCell", forIndexPath: indexPath) as SettingsCustomCell var rowNums:NSNumber = indexPath.row var sectionNums:NSNumber = indexPath.section var selectedRow:Int var selectedSection:Int if(selectedCells.count > 0){ for tuple in selectedCells{ selectedRow = tuple.row selectedSection = tuple.section if(selectedRow == rowNums && selectedSection == sectionNums){ cell.accessoryType = UITableViewCellAccessoryType.Checkmark break }else{ cell.accessoryType = UITableViewCellAccessoryType.None } } }else{ cell.accessoryType = UITableViewCellAccessoryType.None } var keyValue = listOfSectionTitles[indexPath.section] var items: [NSString] = dictionaryOfCellTitles.objectForKey(keyValue) as [NSString] cell.textLabel?.text = items[indexPath.row] return cell } 

处理didSelecteRowAtIndexPath selectedcellvalues

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) if(cell?.accessoryType == UITableViewCellAccessoryType.None){ cell?.accessoryType = UITableViewCellAccessoryType.Checkmark selectedCells.append(row:indexPath.row, section:indexPath.section) }else{ var rowToDelete = indexPath.row var sectionToDelete = indexPath.section for var i=0; i < selectedCells.count; i++ { if(rowToDelete == selectedCells[i].row && sectionToDelete == selectedCells[i].section){ selectedCells.removeAtIndex(i) cell?.accessoryType = UITableViewCellAccessoryType.None } } } tableView.deselectRowAtIndexPath(indexPath, animated: true) } 

NSMutableIndexSet是用来跟踪select状态的更好的对象。

当您返回indexPath的单元格时,还需要检查select状态

 var selectedCells = NSMutableIndexSet() func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { var accessory=UITableViewCellAccessoryType.none if (selectedCells.containsIndex(indexPath.row) { selectedCells.removeIndex(indexPath.row) } else { selectedCells.addIndex(indexPath.row) accessory=.checkmark } if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = accessory } } func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { var cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"CellTable") var accessory=UITableViewCellAccessoryType.none if (selectedCells.containsIndex(indexPath.row) { accessory = .checkmark } view.accessoryType=accessory return view } 
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

您可以从代码中看到重用细胞。 您需要保持所选单元格的状态,并在var view = UITableViewCell ...之后分配辅助视图状态var view = UITableViewCell ...

当你做boolArray.append(false)时,你的错误在cellForRowAtIndexPathcellForrRowAtIndexPath:每次单元格被绘制, 即使它已经被绘制过 。 如果indexPath.row大于indexPath.row的长度,那么可能的修正只会追加false,否则就是您的“模型”,您只需检查它。