对于要从每个部分中选择的行,您必须保留一个selectedCell数组并且对于时间取消选择所有单元格,您必须管理另一个数组isFirsttimeSelection并为 rowCollapse 保留一个boolArray。
接口或头文件实现..
NSArray *sectionArray;
NSMutableDictionary *dictionary;
NSMutableArray *boolArray;
NSMutableArray *selectedCell;
NSMutableArray *isFirsttimeSelection;
查看要使用的数组和字典的初始化。
dictionary=[[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSArray arrayWithObjects:@"Cat",@"Dog",@"Lion",@"Tiger",@"Elephant",nil],@"Animals",[NSArray arrayWithObjects:@"Swallow",@"Parrot",@"Eagle",@"Owl",nil],@"Birds",[NSArray arrayWithObjects:@"Banana",@"Mango",@"Grapes", nil],@"Fruits",nil];
sectionArray=[[NSArray alloc] initWithArray:[dictionary allKeys]];
boolArray=[[NSMutableArray alloc] init];
selectedCell=[[NSMutableArray alloc] init];
isFirsttimeSelection=[[NSMutableArray alloc] init];
for (int i=0;i<sectionArray.count;i++) {
[boolArray addObject:[NSNumber numberWithBool:NO]];
[isFirsttimeSelection addObject:[NSNumber numberWithInt:NO]];
[selectedCell addObject:[NSNumber numberWithInt:0]];
}
tableView 数据源和委托用作:---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([boolArray[section] boolValue]) {
return [[dictionary valueForKey:[sectionArray objectAtIndex:section]] count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.accessoryType=((indexPath.row == [selectedCell[indexPath.section] intValue]) && ([isFirsttimeSelection[indexPath.section] intValue]==1))?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text=[[dictionary valueForKey:[sectionArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
部分的标题视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *headerView=[UIButton buttonWithType:UIButtonTypeCustom];
[headerView setTitle:[sectionArray objectAtIndex:section] forState:UIControlStateNormal];
headerView.tag=section;
headerView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[headerView setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
[headerView.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerView setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[headerView setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[headerView.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[headerView addTarget:self action:@selector(sectionTouched:) forControlEvents:UIControlEventTouchUpInside];
return headerView;
}
单击标题视图上的操作
- (void)sectionTouched:(UIButton *)sender
{
[boolArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:([boolArray[sender.tag] boolValue])?NO:YES]];
[_tblView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:([boolArray[sender.tag] boolValue])?UITableViewRowAnimationTop:UITableViewRowAnimationBottom];
}
最后确实选择了
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedCell[indexPath.section] = [NSString stringWithFormat:@"%i",indexPath.row];
isFirsttimeSelection[indexPath.section]=[NSNumber numberWithBool:YES];
[tableView reloadData];
}
下载它。