继续介绍一个有用的TableView中全选单元格的例子,里面的方法可以用在阅读器中用户对阅读内容来源的选择上,如图所示:
核心代码如下:
[objc]view plaincopy
1.- (void)viewDidLoad
2.{
3. [super viewDidLoad];
4.
5.//创建表视图 _table
6. _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 500) s
tyle:UITableViewStylePlain];
7. _table.delegate = self;
8. _table.dataSource = self;
9. [self.view addSubview:_table];
10.
11.//将字典 "NO"-> @"checked" 装入可变数组中,来控制单元格左边对勾视图
12. _contacts = [NSMutableArray array];
13.for (int i = 0; i <10; i++) {
14.NSMutableDictionary *dic = [NSMutableDictionary dictionary];
15. [dic setValue:@"NO" forKey:@"checked"];
16. [_contacts addObject:dic];
17. }
18.
19.//创建屏幕左上方的"全选"按钮
20. _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
21. [_button setTitle:@"全选" forState:UIControlStateNormal];
22. _button.frame = CGRectMake(10, 10, 100, 50);
23. [_button addTarget:self action:@selector(allSelect:) forControlEvents:UI
ControlEventTouchUpInside];
24. [self.view addSubview:_button];
25.}
[objc]view plaincopy
1.// 全选按钮的触发事件
2.- (void)allSelect:(UIButton*)sender{
3.
4.//将表视图的所有的单元格放在数组里
5.NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[_table indexPaths
ForVisibleRows]];
6.
7.for (int i = 0; i < [anArrayOfIndexPath count]; i++) {
8.
9.// 得到表视图的indexPath
10.NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];
11.//创建每个单元格,
12.LTableViewCell *cell = (LTableViewCell*)[_table cellForRowAtIndexPat
h:indexPath];
13. NSUInteger row = [indexPath row];
14.NSMutableDictionary *dic = [_contacts objectAtIndex:row];
15.
16.// 判断当前按钮的状态是全选还是取消,设置不同的标记
17.if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选
"]) {
18. [dic setObject:@"YES" forKey:@"checked"];
19. [cell setChecked:YES];
20. }else {
21. [dic setObject:@"NO" forKey:@"checked"];
22. [cell setChecked:NO];
23. }
24. }
25.
26.// 两种状态的切换
27.if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]){
28.for (NSDictionary *dic in _contacts) {
29. [dic setValue:@"YES" forKey:@"checked"];
30. }
31.
32. [(UIButton*)sender setTitle:@"取消
" forState:UIControlStateNormal];
33. }
34.
35.else{
36.for (NSDictionary *dic in _contacts) {
37. [dic setValue:@"NO" forKey:@"checked"];
38. }
39. [(UIButton*)sender setTitle:@"全选" forState:UIControlStateNormal];
40. }
41.}
[objc]view plaincopy
1.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPat
h:(NSIndexPath *)indexPath{
2.static NSString * identifier = @"Cell";
3.
4.// 创建单元格
5.LTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ide
ntifier];
6.if (cell == nil) {
7. cell = [[LTableViewCell alloc]initWithStyle:UITableViewCellStyleDefa
ult reuseIdentifier:identifier];
8. }
9. NSUInteger row = [indexPath row];
10.
11.NSMutableDictionary *dic = [_contacts objectAtIndex:row];
12.
13.
14.if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
15. [dic setObject:@"NO" forKey:@"checked"];
16. [cell setChecked:NO];
17.
18. }else {
19. [dic setObject:@"YES" forKey:@"checked"];
20. [cell setChecked:YES];
21. }
22.return cell;
23.}
[objc]view plaincopy
1.- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexP
ath *)indexPath
2.{
3. [tableView deselectRowAtIndexPath:indexPath animated:YES];
4.
5.LTableViewCell *cell = (LTableViewCell*)[tableView cellForRowAtIndexPath
:indexPath];
6.
7. NSUInteger row = [indexPath row];
8.
9.NSMutableDictionary *dic = [_contacts objectAtIndex:row];
10.
11.// 创建单元格左边的打钩视图
12.if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
13. [dic setObject:@"YES" forKey:@"checked"];
14. [cell setChecked:YES];
15. }else {
16. [dic setObject:@"NO" forKey:@"checked"];
17. [cell setChecked:NO];
18. }
19.}
在LTableViewCell中
[objc]view plaincopy
1.// 用 checked 来判断是否加上对勾视图
2.- (void)setChecked:(BOOL)checked{
3.
4.
5.if (checked)
6. {
7. m_checkImageView.image = [UIImage imageNamed:@"Selected.png"];
8.self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/25
5.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
9. }
10.else
11. {
12. m_checkImageView.image = [UIImage imageNamed:@"Unselected.png"];
13.self.backgroundView.backgroundColor = [UIColor whiteColor];
14. }
15. m_checked = checked;
16.
17.
18.}
代码的逻辑简单,通用性强,核心就是用字典来控制对勾视图的显示,对我们以后的视图设计有参考价值。