您的位置:首页 > 其它

如何处理Tableview中cell的单选问题

2015-12-10 12:58 295 查看
首先声明几个属性

@property (nonatomic, strong) NSMutableArray * dataArr;  //数据源
@property (nonatomic, copy) NSString * current;     //数据源中对象
@property (nonatomic, assign) NSInteger currentIndex;   //当前点击的cell的索引


在- (void)viewDidLoad中进行适当初始化

{
self.current = @"";
self.currentIndex = -1;
}


第一种方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identify = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
}
//在cell复用的时候判断是否该cell是被选中的,需要保持选中状态.
if ([self.current isEqualToString:self.dataArr[indexPath.row]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataArr[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//取出之前选择的cell在数据源中的索引,如果是第一次选择,则self.current为nil.
NSInteger index = [self.dataArr indexOfObject:self.current];
if (index == indexPath.row) {
return;
}
//记录之前选择的索引
NSIndexPath * old = [NSIndexPath indexPathForRow:index inSection:0];
//得到当前cell
UITableViewCell * newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.current = [self.dataArr objectAtIndex:indexPath.row];
}
//得到上次选择的cell
UITableViewCell * oldCell = [tableView cellForRowAtIndexPath:old];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
}


第二种方法:

用记录选择的cell的索引self.currentIndex,初始化时设置为-1.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identify = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
}
//在cell复用的时候判断是否该cell是被选中的,需要保持选中状态.
if (self.currentIndex == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataArr[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//取出之前选择的cell在数据源中的索引,如果是第一次选择,则self.current为nil.
if (self.currentIndex == indexPath.row) {
return;
}
//记录之前选择的索引
NSIndexPath * old = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
//得到当前cell
UITableViewCell * newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.currentIndex = indexPath.row;
}
//得到上次选择的cell
UITableViewCell * oldCell = [tableView cellForRowAtIndexPath:old];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: