UITableView选中标记一次只有一行

你会认为这很容易。 有了这个代码,我可以检查表中的多行,但我想要的是一次只检查一行。 如果用户select不同的行,那么我希望旧行只是自动取消选中。 不知道该怎么做。 这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath]; if (theCell.accessoryType == UITableViewCellAccessoryNone) { theCell.accessoryType = UITableViewCellAccessoryCheckmark; } else if (theCell.accessoryType == UITableViewCellAccessoryCheckmark) { theCell.accessoryType = UITableViewCellAccessoryNone; } } 

感谢您的帮助!

最好的方法是在cellForRowAtIndexPath中设置附件types,并使用didSelectRowAtIndexPath来只loggingselect的path,然后调用reloadData。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } 

Swift版本将是:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark } func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None } 

Swift 3 更新

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { tableView.cellForRow(at: indexPath)?.accessoryType = .none } 

您可以创build一个新variables来跟踪在didSelectRowAtIndex处select的索引。

 int selectedIndex; 

在你的cellForRowAtIndexPath

 if(indexPath.row == selectedIndex) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } 

并在你的didSelectRowAtIndex

 selectedIndex = indexPath.row; [tableView reloadData]; 

您不需要(也不应该)在每次select后重新加载表格。

苹果有很好的关于如何pipe理select列表的文档 。 有关示例,请参见清单6-3。

这或多或less与其他一些答案相同,但我想我会添加更多的细节。

你想要做的是将当前选定的IndexPath保存到一个variables,并使用didSelectRowAtIndexPath去除旧的复选标记。 这也是您将添加新复选标记的位置。

您还需要确保在cellForRowAtIndexPath中设置/取消设置复选标记,否则,如果列表很大,并且单元格被重用,则看起来像select了多行。 这是一些其他答案的问题。

请参阅下面的swift 2.0示例:

 // for saving currently seletcted index path var selectedIndexPath: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0) // you wouldn't normally initialize it here, this is just so this code snip works // likely you would set this during cellForRowAtIndexPath when you dequeue the cell that matches a saved user selection or the default func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // this gets rid of the grey row selection. You can add the delegate didDeselectRowAtIndexPath if you want something to happen on deselection tableView.deselectRowAtIndexPath(indexPath, animated: true) // animated to true makes the grey fade out, set to false for it to immediately go away // if they are selecting the same row again, there is nothing to do, just keep it checked if indexPath == selectedIndexPath { return } // toggle old one off and the new one on let newCell = tableView.cellForRowAtIndexPath(indexPath) if newCell?.accessoryType == UITableViewCellAccessoryType.None { newCell?.accessoryType = UITableViewCellAccessoryType.Checkmark } let oldCell = tableView.cellForRowAtIndexPath(selectedIndexPath!) if oldCell?.accessoryType == UITableViewCellAccessoryType.Checkmark { oldCell?.accessoryType = UITableViewCellAccessoryType.None } selectedIndexPath = indexPath // save the selected index path // do whatever else you need to do upon a new selection } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) // if this is the currently selected indexPath, set the checkmark, otherwise remove it if indexPath == selectedIndexPath { cell.accessoryType = UITableViewCellAccessoryType.Checkmark } else { cell.accessoryType = UITableViewCellAccessoryType.None } } 

你不需要重新加载tableView。

看下面的代码:

为你的类声明一个NSIndexPath的lastSelectedIndexPathvariables

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(lastSelectedIndexPath) { UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelectedIndexPath]; [lastCell setAccessoryView:nil]; } UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:currentIndexPath]; [currentCell setAccessoryView:UITableViewCellAccessoryCheckmark]; lastSelectedIndexPath = indexPath; } 

我认为https://stackoverflow.com/a/5960016/928599将帮助你。
我用它,它也适用于deselectRowAtIndexPath!

最简单的方法是保存选定的IndexPath并在cellForRowAtIndexPath中检查它。

附上的是代码:

步骤1:初始化selectedIndexPath selectedIndexPath = [[NSIndexPath alloc] init];

第2步:在cellForRowAtIndexPath

 if([selectedIndexPath isEqual:indexPath]){ [checkMark setHidden:NO]; } else { [checkMark setHidden:YES]; } 

第3步:在didSelectRowAtIndexPath

 selectedIndexPath = indexPath; [tableView reloadData]; 

它应该工作尝试这个Njoy 🙂

尝试这个:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 1 { // un-select the older row if let selected = self.LastSelected { tableView.cellForRowAtIndexPath(selected)?.accessoryType = .None } // select the new row tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark self.LastSelected = indexPath } } 

在Swift中,完美的作品如下:

 lastSelectedIndexPath = NSIndexPath(forRow: 1, inSection: 0)// Row will be the previous selected row func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:LabelsSelectionCell = tableView.dequeueReusableCellWithIdentifier("LabelsSelectionCell", forIndexPath: indexPath) as! LabelsSelectionCell cell.setCellLael(labelOptionsArray[indexPath.row]) if lastSelectedIndexPath == indexPath { cell.setCellCheckedStatus(true) } return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let _ = lastSelectedIndexPath { let lastCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath!) as! LabelsSelectionCell lastCell.setCellCheckedStatus(false) } let currentCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(indexPath) as! LabelsSelectionCell currentCell.setCellCheckedStatus(true) lastSelectedIndexPath = indexPath tableView.deselectRowAtIndexPath(indexPath, animated: true) } 

对于Swift 3以下的工作对我来说:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableVIew.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { tableVIew.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none } 
 Swift iOS var checkedIndexPath : NSIndexPath? // table row which row was selected func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) { tableView.deselectRowAtIndexPath(indexPath, animated: true) println("Section #\(indexPath.section)! You selected cell #\(indexPath.row)!") if (self.checkedIndexPath != nil) { var cell = tableView.cellForRowAtIndexPath(self.checkedIndexPath!) cell?.accessoryType = .None } var cell = tableView.cellForRowAtIndexPath(indexPath) cell?.accessoryType = .Checkmark self.checkedIndexPath = indexPath }// end tableView 

在Swift 2.0中,我使用了这个:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if((lastSelectedIndexPath) != nil) { let lastCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath) lastCell?.accessoryType = UITableViewCellAccessoryType.None } let currentCell = tableView.cellForRowAtIndexPath(indexPath) currentCell?.accessoryType = UITableViewCellAccessoryType.Checkmark lastSelectedIndexPath = indexPath } 

我的方法是select期间取消select其他单元格:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = .... if condition { cell.accessoryType = .checkmark tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.bottom) } else { cell.accessoryType = .none } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { for row in 0...tableView.numberOfRows(inSection: 0) { if row == indexPath.row {continue} tableView.deselectRow(at: IndexPath(row: row, section: 0), animated: true) } if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .checkmark } } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) cell?.accessoryType = .none } 

这是我遇到这个问题时想到的。
这个代码是在最新版本的Swift中实现的,截至今天…
欲了解更多信息和/或扩展文件,请查看这个片段的Github Gist 。

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let sip = selectedIndex { tableView.deselectRow(at: sip, animated: false) } selectedIndex = indexPath } override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { if selectedIndex?.row == indexPath.row { selectedIndex = nil } } 

testing和解决尝试这个完美的工作

将其添加为全局variables

 var selectedIndexPath = NSIndexPath(row: 0, section: 0) 

在didselect方法中添加这个

  // old one check off if indexPath != selectedIndexPath as IndexPath { let oldCell = categorytable.cellForRow(at: selectedIndexPath as IndexPath) if oldCell?.accessoryView != nil { oldCell?.accessoryView = nil } else { let imageName = "checkmark" let image: UIImageView = UIImageView(image: UIImage(named: imageName)) cell.accessoryView = image } } // the new one on if cell.accessoryView == nil { let imageName = "checkmark" let image: UIImageView = UIImageView(image: UIImage(named: imageName)) cell.accessoryView = image } else { cell.accessoryView = nil }