Reputation: 227
I am creating accordion tableview
cell using iOS storyboard
. Here I have maintaining two tableview custom cell classes
and cells
on single storyboard within single tableview.
Now my problem is after selected the row based on plist
storage It will expand (adding) additional rows at under the selected two. I need to use separate tableview cell for selected tableview cell (parent cell)
and expandable cell (child cell)
. By my below code parent and child cell are showing and referring same cells(thats is parent only. So I cant differentiate UI for parent and child.
Need to modify below my logic.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic=[self.itemsInTable objectAtIndex:indexPath.row];
if([dic valueForKey:@"SubItems"])
{
NSArray *arr=[dic valueForKey:@"SubItems"];
BOOL isTableExpanded=NO;
for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}
if(isTableExpanded)
{
[self CollapseRows:arr];
}
else
{
// NEED TO ADD SECOND CELL HERE
NSUInteger count=indexPath.row+1;
NSMutableArray *arrCells=[NSMutableArray array];
for(NSDictionary *dInner in arr)
{
[arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.itemsInTable insertObject:dInner atIndex:count++];
}
[self.main_tableview insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationNone];
}
}
}
Upvotes: 1
Views: 2799
Reputation: 312
DSNestedAccordion does nested tableViews very well. Have a look here
Upvotes: 0
Reputation: 16864
In following code I created multilevel of expandable UITableView
using .plist
file.
Read Data from .plist
file
NSDictionary *dTmp = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
self.arrayOriginal = [dTmp valueForKey:@"Objects"];
self.arForTable = [[NSMutableArray alloc] init];
[self.arForTable addObjectsFromArray:self.arrayOriginal];
Code for UITableView
Delegates and Datasource.
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arForTable count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
[cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
if([d valueForKey:@"Objects"]) {
NSArray *ar=[d valueForKey:@"Objects"];
BOOL isAlreadyInserted=NO;
for(NSDictionary *dInner in ar ){
NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
isAlreadyInserted=(index>0 && index!=NSIntegerMax);
if(isAlreadyInserted) break;
}
if(isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row+1;
NSMutableArray *arCells=[NSMutableArray array];
for(NSDictionary *dInner in ar ) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.arForTable insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
-(void)miniMizeThisRows:(NSArray*)ar{
for(NSDictionary *dInner in ar ) {
NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:@"Objects"];
if(arInner && [arInner count]>0){
[self miniMizeThisRows:arInner];
}
if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
[self.arForTable removeObjectIdenticalTo:dInner];
[self.tblForCollapse deleteRowsAtIndexPaths:[NSArray arrayWithObject:
[NSIndexPath indexPathForRow:indexToRemove inSection:0]
]
withRowAnimation:UITableViewRowAnimationRight];
}
}
}
Download the sample code from here.
Output
May this help to fulfil your requirement.
Upvotes: 1
Reputation: 8486
You would need to design two different table cell views, and instantiate either one in cellForRowAtIndexPath
, depending on if it's a sub-item or a not.
Pretty much
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id item = self.itemsInTable[indexPath.row];
UITableViewCell *cell;
if (item[@"isSubItem"]) {
cell = [tableView dequeueReusableCellWithIdentifier:@"subItemCell"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
}
// ...
return cell;
}
Upvotes: 1