1.设置一个标记来记录选中的行数
@property (nonatomic, strong) NSIndexPath *selectPath; //存放被点击的哪一行的标志
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,记录下之前选择的单元格,并且实时更新
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = (int)[indexPath row];
int oldRow = (int)(_selectPath != nil) ? (int)[_selectPath row]:-1;
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_selectPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
_selectPath = [indexPath copy];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// [self.navigationController popViewControllerAnimated:YES];
}
4.然后在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
}
if (_selectPath == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = self.languageArr[indexPath.row];
return cell;
}
补充:可以在cellForRowAtIndexPath:(NSIndexPath *)indexPath补充实现默认选中某一具体的行