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;
}