2

I have a custom cell and one of its labels can be larger enough to justify adding a "See more" button. So I'm cutting off the label's content to 5 lines and then adding this button that expands the content, if needed.

My question is: Can this be done without using reloadData of the tableView?

I have tried this code on the touch event of the "See more" button (inside the custom cell):

  self.contentView.frame = self.backgroundView.frame = self.accessoryView.frame =  CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height + 80);

Nothing happens! I think I need a way to expand the cell row height and so the table view's content height.

Thanks in advance,


Edition: I checked the solution in link ios - Expand table view cell like Twitter app, I have applied this code in the custom cell (on the touch event):

self.contentExpanded = YES;
UITableView *tableView = [self parentTableView];
NSIndexPath *indexPath = [tableView indexPathForCell:self];


[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

I'm having 2 issues now: 1) The reloadRowsAtIndexPaths is performing a whole reloadData. 2) The custom cell is the one who knows the cell's height. When accesing this method, self.contentExpanded is always equal to NO.

***The method parentTableView was taken from Reference to the parent tableView of a cell


Edition 2: I dont think the issue is on the heightForRow. I can't avoid this method being called for all of the cells...

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath
 {        
    CellObject *object;  
      if(self.searchTableData)
          object = [self.searchTableData objectAtIndex:indexPath.row];
      else
          object = [self.database.cellObjectsFetchedResultsController objectAtIndexPath:indexPath];  


    id cell = [TableViewCellFactory createFromCellObject:object fromPrototypeTable:self.tableView];

    CGFloat height = [cell getHeightRowForCellObject:object];

    return height;

}

And in the custom cell:

  - (CGFloat)getHeightRowForCellObject:(CellObject *)cellObject
  {

CGFloat messageHeight = 0;

if (![cellObject.content isEqualToString:@""])
{
    UILabel *tempMessageLabel = [[UILabel alloc] init];

    tempMessageLabel.font = [UIFont systemFontOfSize:14];
    tempMessageLabel.autoresizingMask =  (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin );
    tempMessageLabel.numberOfLines = 5;
    tempMessageLabel.text = cellObject.content;
    [tempMessageLabel sizeToFit];
    CGFloat lineHeight = [tempMessageLabel.text sizeWithFont:tempMessageLabel.font].height;
    CGSize size = [tempMessageLabel.text sizeWithFont:tempMessageLabel.font
                                    constrainedToSize:CGSizeMake(280, lineHeight * tempMessageLabel.numberOfLines)
                                        lineBreakMode:UILineBreakModeWordWrap];
    messageHeight = size.height;

    if(self.contentExpanded)
        messageHeight += 100;
}
return messageHeight;
}
Community
  • 1
  • 1
  • 2
    I think this has been asked already: http://stackoverflow.com/questions/14967696/ios-expand-table-view-cell-like-twitter-app – Mike D Mar 25 '13 at 15:50
  • Thanks Mike! I'vee been checking questions for a while but haven't found that one. I will check it out and see if it solves my issue. I let you know later. – Rodrigo Ginyaume Mar 25 '13 at 16:00
  • Hi, I have edited my question. I wasnt able to make it work. Maybe I'm doing something wrong. Please help, I can provide more code, if needed. – Rodrigo Ginyaume Mar 25 '13 at 18:36
  • Have you tried removing beginUpdates/endUpdates? – Matt Mar 25 '13 at 18:42
  • Show us your `tableView:heightForRowAtIndexPath:`. – rob mayoff Mar 25 '13 at 19:02
  • Simply adding [myTableView beginUpdates]; [myTableView endUpdates]; to do the famous trick and the table is still being entirely reloaded. – Rodrigo Ginyaume Mar 25 '13 at 19:03
  • Yes Matt, I tried only keeping reloadRowsAtIndexPaths and I got an entire reloadData. – Rodrigo Ginyaume Mar 25 '13 at 19:08
  • You need to store the information about which cells are expanded in your table view delegate or in your model, **not** in your table view cells. The problem is that you're setting the `contentExpanded` flag on a table view cell when the “See More” button is tapped. But then when you need to get the height of the cell, you create an entirely new cell for the same row and ask the new cell for its height. Of course the new cell doesn't share its `contentExpanded` flag with the existing cell. – rob mayoff Mar 26 '13 at 02:36
  • Thanks for your reply. I do realize now how to fix my second issue. However, I'm still receiving invocations to `tableView:heightForRowAtIndexPath:` for all the cells but I get just one call (the expected cell) to `tableView:cellForRowAtIndexPath:`. Is this the expected behaviour? Is the reason because the table view needs to know its new content height? If I only want to re-draw one cell, I would like to avoid executing all the logic that is in the `tableView:heightForRowAtIndexPath:`. – Rodrigo Ginyaume Mar 26 '13 at 13:19

0 Answers0