Search code examples
iosuitableviewexpandable

Can any one tell how to implement expandable/collapse tableview


Here is my screen and code i have attached below.can any one give idea how to implement the screen thanks in advance..in my header section i need show only the tableview cell.........................................

   [1]: https://i.sstatic.net/EVlQs.png

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
{
return [arr count];
}


-(UITableViewCell*)tableView:(UITableView *)tableView    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier =@"cell";
MyOrderCell *cell = (MyOrderCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];


/********** If the section supposed to be closed *******************/
if(cell==nil)
{

    cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}

if (selectedindex==indexPath.row) {

    // do expanded stuff here
    cell.lblcope.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:@"pendents"];
    cell.lblcopieces.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:@"copiece"];
    cell.lblconum.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:@"conum"];
    cell.lblcodate.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:@"codate"];
    cell.lblcoenddate.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:@"coenddate"];
    cell.lbltotl.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:@"cototal"];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

}else
{
    // do closed stuff here

    cell.lblpendnts.text=[arr objectAtIndex:indexPath.row];
    cell.lblpieces.text=[arrpieces objectAtIndex:indexPath.row];
    cell.lblnum.text=[arrnum objectAtIndex:indexPath.row];
    cell.lbldate.text=[arrdate objectAtIndex:indexPath.row];
    cell.lblenddate.text=[arrenddate objectAtIndex:indexPath.row];
    cell.lbltotl.text=[arrttl objectAtIndex:indexPath.row];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

}

return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedindex==indexPath.row) {
    selectedindex=-1;
    [tbldata reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    return;

}

if (selectedindex !=-1) {

    NSIndexPath *prevpath=[NSIndexPath indexPathForRow:selectedindex inSection:0];
    selectedindex=indexPath.row;
    [tbldata reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevpath] withRowAnimation:UITableViewRowAnimationFade];

   }

    selectedindex=indexPath.row;
   [tbldata reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

   }

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
   {
if (selectedindex==indexPath.row) {
    return 100;
   }else
   {
    return 50;
   }
  }

Solution

  • You can use https://github.com/iSofTom/STCollapseTableView

    I have used this in a project. The code written is very clean, so you can also easily customise it to cater your specific needs if any.