iPhone:UITableView CellAccessory复选标记

ios*_*ios 37 iphone objective-c uitableview ios4

在iPhone App中单击表格视图单元格我要显示表格视图单元格附件类型在didSelectRowAtIndexPath上的复选标记我正在编写代码

if(indexPath.row ==0)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
 }
Run Code Online (Sandbox Code Playgroud)

并显示复选标记.

但在我的情况下,我想允许用户一次只检查单元格意味着如果用户选择其他行然后应该检查该行并且之前检查应该是未选中的

我怎样才能做到这一点?

idz*_*idz 169

在实例变量中跟踪检查哪一行.当用户选择新行时,首先取消选中先前检查的行,然后检查新行并更新实例变量.

这里有更多细节.首先添加一个属性以跟踪当前检查的行.如果是这样的话,这是最简单的NSIndexPath.

@interface RootViewController : UITableViewController {
    ...
    NSIndexPath* checkedIndexPath;
    ...
}

...
@property (nonatomic, retain) NSIndexPath* checkedIndexPath;
...

@end
Run Code Online (Sandbox Code Playgroud)

在您cellForRowAtIndexPath添加以下内容:

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else 
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
Run Code Online (Sandbox Code Playgroud)

如何编码您的tableView:didSelectRowAtIndexPath:意愿取决于您想要的行为.如果必须检查行,即,如果用户单击已检查的行,请使用以下内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}
Run Code Online (Sandbox Code Playgroud)

如果您希望允许用户再次单击该行来取消选中该行,请使用以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                    cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    if([self.checkedIndexPath isEqual:indexPath])
    {
        self.checkedIndexPath = nil;
    }
    else
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.checkedIndexPath = indexPath;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 尼斯.仔细思考过的.彻底.很好地使用代码示例.干得好,感谢您的贡献! (6认同)

vis*_*kh7 5

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow)
{
    newCell = [tableView  cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone;
    lastIndexPath = indexPath;  
}
}
Run Code Online (Sandbox Code Playgroud)

有关参考,请参阅此讨论