继续介绍一个有用的TableView中全选单元格的例子,里面的方法可以用在阅读器中用户对阅读内容来源的选择上,如图所示:
核心代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
//创建表视图 _table
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 500) style:UITableViewStylePlain];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
//将字典 "NO"-> @"checked" 装入可变数组中,来控制单元格左边对勾视图
_contacts = [NSMutableArray array];
for (int i = 0; i <10; i++) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:@"NO" forKey:@"checked"];
[_contacts addObject:dic];
}
//创建 屏幕左上方的"全选"按钮
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_button setTitle:@"全选" forState:UIControlStateNormal];
_button.frame = CGRectMake(10, 10, 100, 50);
[_button addTarget:self action:@selector(allSelect:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
// 全选按钮的触发事件
- (void)allSelect:(UIButton*)sender{
//将表视图的所有的单元格放在数组里
NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[_table indexPathsForVisibleRows]];
for (int i = 0; i < [anArrayOfIndexPath count]; i++) {
// 得到表视图的indexPath
NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];
//创建每个单元格,
LTableViewCell *cell = (LTableViewCell*)[_table cellForRowAtIndexPath:indexPath];
NSUInteger row = [indexPath row];
NSMutableDictionary *dic = [_contacts objectAtIndex:row];
// 判断当前按钮的状态是全选还是取消,设置不同的标记
if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]) {
[dic setObject:@"YES" forKey:@"checked"];
[cell setChecked:YES];
}else {
[dic setObject:@"NO" forKey:@"checked"];
[cell setChecked:NO];
}
}
// 两种状态的切换
if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]){
for (NSDictionary *dic in _contacts) {
[dic setValue:@"YES" forKey:@"checked"];
}
[(UIButton*)sender setTitle:@"取消" forState:UIControlStateNormal];
}
else{
for (NSDictionary *dic in _contacts) {
[dic setValue:@"NO" forKey:@"checked"];
}
[(UIButton*)sender setTitle:@"全选" forState:UIControlStateNormal];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"Cell";
// 创建单元格
LTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[LTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSUInteger row = [indexPath row];
NSMutableDictionary *dic = [_contacts objectAtIndex:row];
if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
[dic setObject:@"NO" forKey:@"checked"];
[cell setChecked:NO];
}else {
[dic setObject:@"YES" forKey:@"checked"];
[cell setChecked:YES];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
LTableViewCell *cell = (LTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
NSUInteger row = [indexPath row];
NSMutableDictionary *dic = [_contacts objectAtIndex:row];
// 创建单元格左边的打钩视图
if ([[dic objectForKey:@"checked"] isEqualToString:@"NO"]) {
[dic setObject:@"YES" forKey:@"checked"];
[cell setChecked:YES];
}else {
[dic setObject:@"NO" forKey:@"checked"];
[cell setChecked:NO];
}
}
在LTableViewCell中
// 用 checked 来判断是否加上对勾视图
- (void)setChecked:(BOOL)checked{
if (checked)
{
m_checkImageView.image = [UIImage imageNamed:@"Selected.png"];
self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
}
else
{
m_checkImageView.image = [UIImage imageNamed:@"Unselected.png"];
self.backgroundView.backgroundColor = [UIColor whiteColor];
}
m_checked = checked;
}
代码的逻辑简单,通用性强,核心就是用字典来控制对勾视图的显示,对我们以后的视图设计有参考价值。