新手一枚
问题如下:
我想用 TableView 做一个可以多项选择的表格,然后把 tableView的allowsMultipleSelection 属性设为了YES;
_tableView.allowsMultipleSelection = YES;
在 didSelectRowAtIndexPath 和 didDeselectRowAtIndexPath 方法里面使用了如下方法实现了点击单元格然后用check mark 标记的方式。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
但是运行起来发现单元格重用问题会影响到check mark的标记。
具体表现是点击第一个单元格之后,滚动页面,后面重用的单元格也会显示check mark的对号标记,但是单元格是未选中状态。
我怀疑是在 cellForRowAtIndexPath 这个函数上的调用出了问题。
请问有没有解决这个问题的更好的写法?
万分感谢。
附部分相关代码:
//tableVie的创建
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kTopViewHeight, kScreenWidth, kTableViewHeight) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.allowsMultipleSelection = YES;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
//tableVieCell的创建
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.textLabel.text = _dataArray[indexPath.row];
return cell;
}
首先表扬一下,问题提得很棒~ 格式整齐,代码贴得很全,赞一个。
其次,这个思路不对。
你想要做一个多项选择的表格,最终目的不光是为了给选中的 cell 打勾,更是为了得到用户选择的数据。如果你得不到哪一项选中了、哪一项没选中的数据,就算打勾打正确了有什么用呢?
所以正确的思路就应该是围绕着记录选中/没选中的数据出发。在
didSelectRow
、didDeselectRow
里操作数据的变化,然后通过 reloadData 显示出来。设一个
NSMutableArray
属性,元素个数跟你的_dataArray
一样,初始化里面存的都是0
。在
didSelectRowAtIndexPath:
里:在
didDeselectRowAtIndexPath
里:在
cellForRow
里: