藍色是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即可