ios - 具有动态数据初始化的 UiTableviewCell 未正确加载

标签 ios objective-c xcode uitableview uitableviewrowaction

在我的单元格中,数据正在加载,但在我向下滚动之前它都是乱七八糟的,然后如果我向后滚动所有格式正确。因此,当我加载的每一个 uitableview 数据列表都被搞砸了,一旦我开始滚动并向上滚动,它就会到位并正确显示。请帮忙。

我的代码

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    for(UIView *v in [cell subviews])
    {
        if([v isKindOfClass:[UILabel class]])
            [v removeFromSuperview];
        if ([v isKindOfClass:[UIImageView class]])
            [v removeFromSuperview];
    }
UIView *CellBGView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, cell.frame.size.width-20, cell.frame.size.height-10)];
    CellBGView.backgroundColor = [UIColor whiteColor];

    UIImageView *divider = [[UIImageView alloc] initWithFrame:CGRectMake(CellBGView.frame.size.width-155,       CellBGView.frame.size.height-140, 150, 150)];
    divider.image = [UIImage imageNamed:@"icon_alpha.png"];
    [CellBGView addSubview:divider];
    ......
    return cell;
}

这是我打开列表时和滚动表格之前的输出

enter image description here

滚动后一切就绪

enter image description here

最佳答案

我有一个解决方案。

  • 首先,创建UITableViewCell的子类(例如,将其命名为DemoTableViewCell)

  • DemoTableViewCellinitWithStyle 方法中,将图像和标签添加到单元格。

  • cellForRowAtIndexPath 中,您不需要添加或删除图像和标签。只需为它们更改颜色或设置图像即可。

关于ios - 具有动态数据初始化的 UiTableviewCell 未正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47072766/

相关文章:

ios - 是否可以子类化 UITableView 并使其具有基于单元格数量的固有内容大小?

ios - CocoaPods 重复问题

ios - 如何使用自定义标题将单元格分成几个部分?

java - PhoneGap/Cordova 和原生 Android/iOS 后端

objective-c - 桥接 Objective-C header 会导致 Swift 编译器错误

ios - 如何使用 InAppSettingsKit 委托(delegate)重新配置已更改的应用程序设置

ios - 应用程序标识符不匹配 - 应用程序 ID XCode

ios - Xcode 7 Beta 4 - 每次运行该项目时都找不到 Storyboard

ios - 相机在 iOS 的 UIImagepickercontroller 中出现黑屏

iphone - 有没有办法在 UIViewController 中锁定背景图像?