ios - TableView 在滚动时更改单元格内容

标签 ios objective-c xcode cocoa-touch

我知道,这个问题被问了好几次,每次都被回答超过 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;

最佳答案

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

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/

相关文章:

ios - UIImagePickerController 在拍照时是横向的,但在预览时是纵向的

iOS - 将角标(Badge)添加到 UITableViewCell

ios - 无法创建可重用 View 的实例

ios - NativeScript 找不到模块组件/主页

ios - iOS/iPhone 应用程序的(文件)格式是什么?

ios - 如何在其他开发人员设备上安装我的 xcode 项目进行测试

swift - 将数据添加到 firebase 中的特定 UID

iphone - 数据模型中的 Char* 内存被回收?

objective-c - Core-Plot 中 y 轴标签的四舍五入数字

ios - 找不到 PDF 标题 : `%PDF' not found