如何在UITableViewCell中添加复选标记

时间:2012-07-15 08:03:57

标签: objective-c xcode cocoa-touch uitableview

假设我有一个小的tableview(没有滚动),当用户点击一个选项时,它会改变应用程序中的一些设置。我用过这个函数:

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      //some code here
}

现在我想对所选的选项使用UITableViewCellAccessoryCheckmark,我的意思是它应该在用户选择的单元格中显示一个复选标记(并从之前选择的选项中删除复选标记)。我该怎么做?

2 个答案:

答案 0 :(得分:8)

怎么样

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
  for (UITableViewCell *cell in [tableView visibleCells]) {
    cell.accessoryType = UITableViewCellAccessoryNone;
  }

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

答案 1 :(得分:0)

只需使用accessoryType的{​​{1}}属性。

UITableViewCell

像这样:

@property(nonatomic) UITableViewCellAccessoryType accessoryType