4

Currently when scrolling upwards in my tableView I have found a strange and annoying bug. When I decide to release the "scroll", meaning I am dropping "the scroll" so the "View" will return to its normal position showing all of the TableView's content, some of my cell's can for some reason re-size themselves on the width. I have no clue why this occur or what the problem might be.

My cell's (commentLabel) are customized to fitSize depending on the height of the label in my forum. I assume the problem may be in how I am trying to customize my cell's content. I will post my relevant code and also post to pictures below.

Before starting to drag the scroll upwards: http://tinypic.com/view.php?pic=2rfsum9&s=6

After release/droped the scroll again to its normal position. Now one of the cell's changed: http://tinypic.com/view.php?pic=swxnqv&s=6

Code:

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ForumthreadCell";
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

    [aliasLabel setText:item.alias];
    [commentLabel setText:item.comment];
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

    commentLabel.numberOfLines = 0;
    [commentLabel sizeToFit];

    return cell;

}

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

    CGSize maximumSize = CGSizeMake(labelWidth, 10000);

    //provide appropriate font and font size
    CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:12.0f]
                     constrainedToSize:maximumSize
                         lineBreakMode:UILineBreakModeTailTruncation];
    return labelHeighSize.height;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    CGFloat commentTextHeight = [self getLabelHeightForText:item.comment andWidth:162];
    return commentTextHeight + 39;

}

EDIT

The new code:

CGRect frame = commentLabel.frame;
frame.size.width = kLabelWidth;
frame.size.height = 10000;
commentLabel.frame = frame;

frame = commentLabel.frame;
frame.size.height += kOffset;
commentLabel.frame = frame;

But I have done something wrong since now the text from the commentLabel is not getting displayed correctly, can anyone se my fault here in how I try to set the frame? This is what the cell's look like now http://oi50.tinypic.com/2w5u0s6.jpg

The new full code:

static const CGFloat kLabelWidth = 162;
static const CGFloat kOffset = 39;

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ForumthreadCell";
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Feedback *item = [self.items objectAtIndex:indexPath.row];

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

    [aliasLabel setText:item.alias];
    [commentLabel setText:item.comment];
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

    CGRect frame = commentLabel.frame;
    frame.size.width = kLabelWidth;
    frame.size.height = 10000;
    commentLabel.frame = frame;

    frame = commentLabel.frame;
    frame.size.height += kOffset;
    commentLabel.frame = frame;

    commentLabel.numberOfLines = 0;
    [commentLabel sizeToFit];

    return cell;

 }
Jesper Martensson
  • 1,238
  • 3
  • 18
  • 44
  • in `heightForRowAtIndexPath` what is `getLabelHeightForText` returning? is there any chance `item.comment` can be nil and it returns zero? Remember that cells are re-used. – yfrancis Nov 05 '12 at 06:51

1 Answers1

3

You should adjust label frames before reusing cells. In your case you are changing cells height while reusing it in

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

but you should also adjust label's frame according to cell's height so that it will not go beyond cell's bound.

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
Rahul Wakade
  • 4,765
  • 2
  • 23
  • 25
  • Try this- [commentLabel setFrame:CGRectMake(commentLabel.frame.origin.x, commentLabel.frame.origin.y, kLabelWidth, [self getLabelHeightForText:item.comment andWidth:kLabelWidth])] – Rahul Wakade Nov 05 '12 at 10:42
  • Thanx for the answer, my problem now is the height on the cells, for some reason they seem to be completly random. http://oi50.tinypic.com/2w5u0s6.jpg Any thoughts on this ? – Jesper Martensson Nov 05 '12 at 11:02
  • Either you remove offset(commentTextHeight + 39) that you are setting in heightForRowAtIndexPath or add same offset in commentLabel's height. [commentLabel setFrame:CGRectMake(commentLabel.frame.origin.x, commentLabel.frame.origin.y, kLabelWidth, [self getLabelHeightForText:item.comment andWidth:kLabelWidth] + 39)] – Rahul Wakade Nov 05 '12 at 13:11