For row to be selected from each section you have to keep an array of selectedCell And for time deselected all the cells you have to manage one more array that is isFirsttimeSelection And keep a boolArray for rowCollapse.
Interface or header file implementation..
NSArray *sectionArray;
NSMutableDictionary *dictionary;
NSMutableArray *boolArray;
NSMutableArray *selectedCell;
NSMutableArray *isFirsttimeSelection;
have a look at initialization of arrays and dictionary to be used.
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 datasources and delegates to be used as:---
- (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;
}
Header view for section
- (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;
}
Action on header view clicked
- (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];
}
And at last did select
- (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];
}
Download it.