MyJBMe
MyJBMe

Reputation: 2236

TableView changes cell content while scrolling

I know, this is asked several times and it is answered everytime more than 1 time, but no one, of these solutions helped myself!

When my TableView opens, I can see one row of text, instead of >10! So I scroll down and 2 cells later its presented the right way! If I scroll up again, I can see the cell with this single line, is presenting everything right, too! So, here is my question: What went wrong in my code?!

This is my cellForRowAtIndexPath:

NSString *CellIdentifier = @"Cell";
    AACTickerYellowCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[AACTickerYellowCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString * text = [self getTextAtIndex:indexPath];

    CGSize size = [self frameForText:text sizeWithFont:nil constrainedToSize:CGSizeMake(260.0f, CGFLOAT_MAX)];

    [cell.textView setFrame:CGRectMake(10, 5, 260, size.height + 20)];
    cell.textView.text = text;

    return cell;

Upvotes: 2

Views: 1573

Answers (2)

Mirko Catalano
Mirko Catalano

Reputation: 3850

Try like that and your problem disappear:

NSString *CellIdentifier = @"Cell";
    AACTickerYellowCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[AACTickerYellowCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString * text = [self getTextAtIndex:indexPath];

    dispatch_async(dispatch_get_main_queue(), ^{

        CGSize size = [self frameForText:text sizeWithFont:nil constrainedToSize:CGSizeMake(260.0f, CGFLOAT_MAX)];

        [cell.textView setFrame:CGRectMake(10, 5, 260, size.height + 20)];
        cell.textView.text = text;
    });
    return cell;

Upvotes: 1

Mil0R3
Mil0R3

Reputation: 3956

Maybe you missed row?

NSString * text = [self getTextAtIndex:indexPath.row]

Upvotes: 0

Related Questions