2

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

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

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

4 回答 4

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];
}

或者

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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

    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
于 2012-06-09T11:07:30.350 回答
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;
}
于 2014-02-02T14:47:34.360 回答
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)
}
于 2015-12-06T17:28:00.457 回答
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;

于 2012-06-09T11:04:08.553 回答