Search code examples
iosobjective-cuitableviewrowssections

Expanding And Collapsing Sections


Can somebody suggest me how to implement the following in Objective C?

When I click on a section rows for that particular section alone expand. How do I know which section is clicked and how do I return rows count for that particular section?

Note: I have kept a button in my section. And when i click rows of all the sections get expanded and again when i click on a section rows of all the sections get collapsed.


Solution

  • Note :- My TableView Cell height is 126, but when i run my application, TableCell height is 75 (As i mention below), when i pressed on TableCell than automatically TableCell height expand.

    See :- int myCurrentSelection=9999; // Set globally.

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    int myrow = (int)indexPath.row;
    
    if (myCurrentSelection == myrow) {
        myCurrentSelection = 9999;
        cell.contentView.backgroundColor = [UIColor whiteColor];
    } else {
        myCurrentSelection = myrow;
        cell.contentView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    }
    [tableView beginUpdates];
    [tableView endUpdates];
    [self.tblView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
    
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGSize boundingBox = [mutArrMessages [indexPath.row][@"message"] boundingRectWithSize:CGSizeMake((self.tblView.frame.size.width - 86),MAXFLOAT)
                                                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                                                               attributes:@{NSFontAttributeName:[UIFont helveticaNeueLightFontOfSize:17.0]}
                                                                                  context:nil].size;
    CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
    if ([indexPath row] == myCurrentSelection) {
        return (MAX((size.height + 46),76)) + 50;
    }
    return (MAX((size.height + 46),76));
    }
    
    
    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {    
     return 76;
    }