将UITableView SelectedCell附件类型设置为选中标记

时间:2012-08-09 06:03:53

标签: xcode uitableview

我将UITableview选择的单元格样式设置为chek mark但问题是它显示选中单元格时的检查但是当我们选择新单元格时,另一个单元格也会先检查标记。我希望只显示所选单元格的一个复选标记

   UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];


} 
else {
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];

1 个答案:

答案 0 :(得分:0)

在返回单元格之前将此代码添加到cellForRowAtIndexPath;

    if (self.selectedIndexPath != nil && self.selectedIndexPath.row == indexPath.row
                && self.selectedIndexPath.section == indexPath.section) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

此代码发送到didSelectRowAtIndexPath

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if (self.selectedIndexPath != nil) {
        // Uncheck previously selected cell
        UITableViewCell *prevSelectedCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
        [prevSelectedCell setAccessoryType:UITableViewCellAccessoryNone];
    }
    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    self.selectedIndexPath = indexPath;

    [tableView deselectRowAtIndexPath:indexPath animated:NO];