Для ряда должны быть выбраны из каждого раздела вы должны сохранить массив selectedCell И времени отменяется все ячейки, которые вы должны управлять еще один массив, is isFirsttimeSelection И сохранить boolArray для rowCollapse.
интерфейс или файл заголовок реализация ..
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 *[email protected]"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];
}
Download it.
взглянуть на эту ссылку https://github.com/gcamp/GCRetractableSectionController – Warewolf
@ HERCULES Я уже пытался, что один. Не использовать. Даже он не отображает его таблицу в моем ViewController. – Praveenkumar
u хотите выбрать только одну строку из каждой секции ИЛИ только одну строку из целого tableView? – Warewolf