1

I have a UITableView with multiple UILabels. The issue is that the text in these cells change dynamically as I receive data from the server. It works fine when I load the view controller. But as I scroll, the height of the cells are not updated as heightForRowAtIndexPath is only called once.

Here are the screenshots:

Before Scrolling

After Scrolling

As I've shown in the screenshot, the question label reduces in size which leads to a gap (shown by arrow).

Here's my cellForRowAtIndexPath:

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

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil)
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }

    cell.question.autoDetectLinks = YES;

    // Used to populate cell from NSDictionary
    [self setDataToCell:cell AtIndexPath:indexPath];

    return cell;
}

Here's my custom cell's layoutSubviews:

- (void) layoutSubviews
{
    CGRect frame = self.question.frame;
    frame.size.width = 277.0f; //you need to adjust this value
    self.question.frame = frame;
    self.question.numberOfLines = 2;

    [self.question sizeToFit];
    // Place time below question
    CGRect timeFrame = self.time.frame;
    timeFrame.origin.y = self.question.frame.origin.y + self.question.frame.size.height + 5;
    self.time.frame = timeFrame;
    [self.time sizeToFit];
}

So to tackle this situation I called

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[_tableIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; 

in - (void) scrollViewDidScroll:(UIScrollView *)scrollView This solves my problem but reduces the performance and the elements jump around before settling even after setting the animation as UITableViewRowAnimationNone. Is there a better way of doing it? Should I call reloadRowsAtIndexPaths somewhere else?

Thanks.

Anil
  • 2,394
  • 2
  • 32
  • 49

1 Answers1

0

Ok here come the edited answer: The problem is your sizeToFit in your layoutSubviews. Just follow this link to resolve your issue: here

If you also want the cell and the label to dynamically resize to their corresponding text but at the same time your timelabel to be directly underneath it you will have to determine the size of your uilabel based on the text, font and font-size. See here for more information: here

Community
  • 1
  • 1
dehlen
  • 7,141
  • 4
  • 38
  • 68
  • I'm doing that. But that doesn't update the frames of the cells that are not visible. – Anil Dec 17 '13 at 08:19
  • Is `numberOfLines` set to 0 for the label ? Where do you create the label, the cellForRowAtIndexPath does not help since you don't use the label in this code. – dehlen Dec 17 '13 at 08:27
  • I've done that in my layoutSubviews in my customcell class. Updating the question now. – Anil Dec 17 '13 at 08:29
  • Sorry. That doesnt help – Anil Dec 17 '13 at 08:33
  • whats with the answer in the link i provided if you change sizeToFit does that help ? – dehlen Dec 17 '13 at 08:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43310/discussion-between-anil-and-david-ehlen) – Anil Dec 17 '13 at 08:37