OGeek|极客世界-中国程序员成长平台

标题: ios - TableView 在滚动时更改单元格内容 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:52
标题: ios - TableView 在滚动时更改单元格内容

我知道,这被问了好几次,每次都回答超过 1 次,但没有一个,这些解决方案对我自己有帮助!

当我的 TableView 打开时,我可以看到一行文本,而不是 >10!所以我向下滚动,然后 2 个单元格以正确的方式呈现!如果我再次向上滚动,我可以看到带有这一行的单元格,也显示一切正确! 所以,这是我的问题:我的代码出了什么问题?!

这是我的 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;



Best Answer-推荐答案


这样尝试,你的问题就会消失:

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;

关于ios - TableView 在滚动时更改单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507236/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4