【问题标题】:Customized UITableViewCell dissappears自定义 UITableViewCell 消失
【发布时间】:2010-01-12 15:31:12
【问题描述】:

我见过很多人遇到类似的问题,但他们的解决方案要么没有帮助,要么太不一样了。

我的问题是,我有一个自定义的 UITableViewCell、自定义大小、图像和内容。当我向上或向下滚动然后再次返回时,某些单元格中的文本会消失。这似乎是随机发生的。

我已阅读有关“出队”问题的信息,但要么我弄错了,要么它不属于我的情况......

无论如何,这是我的 Cells 的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;


cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:
                        [[NSBundle mainBundle]  pathForResource:
                         [[shopItems  objectAtIndex:indexPath.row] name] ofType:@"jpg"]];

UITextField *temp= [[UITextField alloc] initWithFrame:cell.frame];
temp.text = [[shopItems objectAtIndex:indexPath.row] name];
temp.textColor = [UIColor whiteColor];
temp.textAlignment = UITextAlignmentCenter; 


UIImageView *cellView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:
                                                                   [[NSBundle mainBundle] pathForResource:@"smalltabs_wide_bg"  ofType:@"png"]]];

[cellView addSubview:temp];

cell.backgroundView = cellView;



return cell;
}

您对此有任何(!)想法吗?

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    可能的问题是您每次(重新)使用单元格时都会将 UITextField 和 UIImageView 添加到单元格中。你的代码应该看起来像

     {
       ...
       if (cell == nil) {
           cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease];
    
       // Insert all subviews to cell here
       }
    
       // setup cell's subviews here
       ...
       }
    

    还要注意代码中的所有内存泄漏 - 当您使用 alloc 创建某些内容时,您必须在某处释放它。

    【讨论】:

    • 就是这样,我想我应该更正确地阅读该指南。感谢您的所有建议 - 我还修复了记忆问题。
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      一个问题是您没有释放临时 UITextField,这将导致内存泄漏并可能导致问题。

      【讨论】:

      • 是的,我忘记发布它了,但现在我修复了它,问题仍然存在。
      【解决方案4】:

      分享我的经验:

      有类似的问题。我的应用中有很多表格,其中一个表格中的数据会随机消失,最终整个表格都会消失。

      对我来说,问题是我正在使单元格出列,并为所有单元格提供相同的“唯一”ID。所以几个表共享相同的 id,我相信它们相互冲突并弄乱了我正在查看的单元格。

      为每个表提供它自己的唯一标识符为我解决了这个问题。 (呸!)

      (也在这里发布,可能重复:UITableView custom cell images disappear after scrolling.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        相关资源
        最近更新 更多