相信你們在作項目時有遇到須要實現這種功能---實現單選某一個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的複用
但願對初學者有幫助到!