蓝色是tableViewCell的默认前景色(tintColor),所以我们设置cell.tintColor = [UIColor redColor];就可以改前景色为红色了,同样那个accessoryCheckmark的颜色就变成红色了。
首先我们公开一个属性
@property(nonatomic,strong)NSIndexPath *lastPath;
主要是用来接收用户上一次所选的cell的indexpath
在cellForRowAtIndexPath中加入
-
if (self.selected == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
随后添加action
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selected inSection:0]];
selectedCell.accessoryType = UITableViewCellAccessoryNone;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selected = indexPath.row;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
如需自定义checkmark的图片 设置cell.accessoryView为自己所需的UIImageView即可