自定义的UITableViewCell消失

问题描述:

我见过一些人有类似的问题,但他们的解决方案没有帮助,或者它太不同了。自定义的UITableViewCell消失

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

我已阅读有关“出列” -issue但无论是我听错了或者它不落入我的情况....

不管怎么说,继承人为我的单元格中的代码:

- (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; 
} 

你有什么想法吗?

一个问题是,你不释放你的临时UITextField,这将导致内存泄漏,并可能导致问题。

+0

真实的,我忘了将其释放,但现在,我固定的,问题仍然存在。 – Icky 2010-01-12 15:41:52

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

{ 
    ... 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    // Insert all subviews to cell here 
    } 

    // setup cell's subviews here 
    ... 
    } 

而且还介意在你的代码的所有内存泄漏 - 当你创建页头的东西,你必须某处释放它。

+0

就是这样,我想我应该更正确地阅读关于该指南。感谢所有的建议 - 我也解决了这个问题。 – Icky 2010-01-12 16:42:29

分享我的经验:

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

对我来说,问题是我将细胞排出,给予他们所有的“唯一”ID。所以有几张桌子共用同一个ID,我相信它们是相互冲突的,并且搞乱了我所看到的单元格。

给每个表都是自己的唯一标识符解决了我的问题。 (!DAH)

(登载在这里太,可能是重复的:UITableView custom cell images disappear after scrolling.