I use auto layout and display a table with custom dynamic cells. Basically table displays chat between 2 persons. Thus, text message varies for each cell.
The issue here is that first cells are displayed and then within a second, its contents are resized. This is clearly visible and looks bad.
See below images to get idea of how it looks before and after resizing.
Before resizing :
After resizing :
I know there is a similar question on SO, but its answer does not really satisfy the need. I want to avoid overriding layoutSubviews()
method as I don't think its a good enough solution.
I tried below code to unhide cell's contentView
once they are displayed. But it does not work.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// At the begining
cell.contentView.hidden = NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
// Before returning cell
cell.contentView.hidden = YES;
return cell;
}
Is there a way to delay displaying cell until they are resized to fit their contents?
UPDATE :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];
cell.textString.text = ...;
cell.textString.frame = ...;
cell.timeLabel.text = ...; // set timeLabel to display date and time
cell.userLabel.text = ...; // set userLabel to display userName
if (displaying cell where message is sent by user)
{
// Set image for sender
cell.chatCellBackground.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];
cell.chatCellBackground.frame = ...;
}
else
{
// set bubble for receiver
cell.chatCellBackground.image = [[UIImage imageNamed:@"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];
cell.chatCellBackground.frame = ...;
}
}
[cell setNeedsLayout];
[cell layoutIfNeeded];
return cell;
}