1

I have cells that expand by changing their height

I want to open more information like small view of every cell selection , expanding table view cell.

We selected the cell have to expand and close again while we clicked again.enter image description here

I am using below code , it is working fine but how to show hidden view on particular cell position.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  UILabel *label = nil;

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
       // cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier] ;
    }

    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:indexPath.row];
    //If this is the selected index then calculate the height of the cell based on the amount of text we have
    if(selectedIndex == indexPath.row)
    {
       UILabel *cellLabeltotaldays12=[[UILabel alloc]initWithFrame:CGRectMake(520, 80, 100, 12)];
       cellLabeltotaldays12.font =[ UIFont systemFontOfSize:14];
       cellLabeltotaldays12.text=student.totaldays;
       [cell addSubview:cellLabeltotaldays12];

   }
   else {

     //Otherwise just return the minimum height for the label.
   }
   return cell;
}   
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

     //The user is selecting the cell which is currently expanded
     //we want to minimize it back
     if(selectedIndex == indexPath.row)
     {
        selectedIndex = -1;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        return;
     }

     //First we check if a cell is already expanded.
     //If it is we want to minimize make sure it is reloaded to minimize it back
     if(selectedIndex >= 0)
     {
         NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
         selectedIndex = indexPath.row;
         [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
     }

     //Finally set the selected index to the new selection and reload it to expand
     selectedIndex = indexPath.row;
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
HRM
  • 2,097
  • 6
  • 23
  • 37
user 1035414
  • 77
  • 11
  • Try these links : http://stackoverflow.com/questions/19855832/expanding-and-collapsing-table-view-cells-in-ios http://stackoverflow.com/questions/4635338/uitableviewcell-expand-on-click http://stackoverflow.com/questions/15109099/expand-uitableview-cell-in-uitableviewstyleplain – Samkit Jain Mar 11 '14 at 09:05
  • Hey u have to search first in google. On this topic there are lots of solutions. like one url that is available in stackoverflow exactly matches what your looking. http://stackoverflow.com/questions/19855832/expanding-and-collapsing-table-view-cells-in-ios – Thukaram Mar 11 '14 at 09:02

2 Answers2

3

You need to change the height in heightForRow method as per your requirement .

Samkit Jain
  • 2,523
  • 16
  • 33
1

Try like this

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

    NSString *text = [Comments_text objectAtIndex:indexPath.row];

    CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height+40, 60.0f);

    return height + (CELL_CONTENT_MARGIN * 2);


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"commentcell";

    CommentsCell1 *cell = [Comments_table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CommentsCell1 alloc] initWithFrame:CGRectZero] ;
        cell.userInteractionEnabled=YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }




        NSString *text = [Comments_text objectAtIndex:indexPath.row];

        CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

        [cell.Comment_Text setText:text];
        [cell.Comment_Text setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN+cell.Comment_Emailimg.frame.origin.y+cell.Comment_Emailimg.frame.size.height, tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), MAX(size.height,20.0f))];


    }

    return  cell;

}
Muralikrishna
  • 1,044
  • 10
  • 17