有时候需要显示cell的时候,第一个右边就是打勾的状态,所以自己尝试的写了一个,仅供大家参考;共同学习,交流。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if (indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.indexPath = indexPath;
}
cell.textLabel.text = @"111";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
if (self.indexPath && self.indexPath != indexPath) {
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.indexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
self.indexPath = indexPath;
}
self.indexPath是定义的一个NSIndexPath类型的变量,用来保存每次选中cell的位置。
if中self.indexPath != indexPath的判断是为了处理点击同一个cell,勾号消失的bug