xcode 只选择一个单元格 UITableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10960366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:32:56  来源:igfitidea点击:

Select only one cell UITableView

objective-cxcode

提问by Alessandro

I have a table view with numerous cells. When I press one, a tick appears beside it, but when I select another, the tick on the previous one remains, and a new tick is added on the current cell. so two cells are ticked, but only one at a time must be ticked!!

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

I tried this, but it does not work:

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

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

回答by Mani

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

回答by cwRichardKim

An alternative:

替代:

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

This physically goes through all of the rows and disables them. Kind of brute force, but it works. If you want a version that doesn't allow deselecting,

这在物理上遍历所有行并禁用它们。有点蛮力,但它有效。如果你想要一个不允许取消选择的版本,

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

回答by Rob Norback

The quick fix in Swift:

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)
}

回答by Suhaiyl

In the tableviewdatasource you need to follow this:

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

the lastindex is property(retain) NSIndexPath* lastIndexPath;

lastindex 是 property(retain) NSIndexPath* lastIndexPath;