只选择一个单元格UITableView

时间:2012-06-09 10:51:23

标签: objective-c xcode

我有一个包含大量单元格的表视图。当我按下一个时,旁边会出现一个勾号,但当我选择另一个时,前一个勾号仍然存在,并在当前单元格上添加一个新的勾号。所以勾选了两个单元格,但一次只能勾选一个单元格!!

我试过这个,但它不起作用:

if (cell.selected = YES) {

[cell setSelected:NO animated:YES];
[cell setAccessoryType:UITableViewCellAccessoryNone];

}else if(cell.selected = NO){

    [cell setSelected: YES animated:YES];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }

4 个答案:

答案 0 :(得分:3)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

int newRow = [indexPath row];
int oldRow = [lastIndexPath row];

if (newRow != oldRow)
{
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                                                indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                                                lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;

        lastIndexPath = indexPath;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

OR

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

答案 1 :(得分:1)

替代方案:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (int i = 0; i<yourArray.count;i++)
    {
        if(i!=[indexPath row])
        {
            cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

这会物理地遍历所有行并禁用它们。有点暴力,但它的工作原理。如果您想要一个不允许取消选择的版本,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (int i = 0; i<yourArray.count;i++)
    {
        cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

答案 2 :(得分:1)

Swift中的快速修复:

 var lastIndexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let newRow = indexPath.row
    let oldRow = lastIndexPath.row

    if oldRow != newRow {
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
        tableView.cellForRowAtIndexPath(lastIndexPath)?.accessoryType = .None

        lastIndexPath = indexPath
    }

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

答案 3 :(得分:0)

在tableviewdatasource中你需要遵循:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil )
    {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.lastIndexPath = indexPath;

    [tableView reloadData];
}

lastindex是property(retain)NSIndexPath * lastIndexPath;