相信你们在作项目时有遇到须要实现这种功能---实现单选某一个cell表示选中atom
这个功能的实现只须要在两个方法中code便可spa
首选咱们公开一个属性 code
@property(nonatomic,strong)NSIndexPath *lastPath;而且对其synthesizetable
主要是用来接收用户上一次所选的cell的indexpath
ast
第一步:在cellForRowAtIndexPath:方法中实现以下代码class
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{select
NSInteger row = [indexPath row];sso
NSInteger oldRow = [lastPath row];方法
if (row == oldRow && lastPath!=nil) {im
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
第二步:在didSelectRowAtIndexPath:中实现以下代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int newRow = [indexPath row];
int oldRow = (lastPath !=nil)?[lastPath row]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Ok,能够收工了,这样实现以后的效果是每次单击一个cell会作一个选中的标志而且托动表视图时也不会出现checkmark的复用
但愿对初学者有帮助到!