2010-04-20 61 views
2

我有一个高单元格的表格视图。身高约300。单元格是自定义的,并有一个关联的笔尖。在cellForRowAtIndexPath中,我使用indexPath.row访问数组中的对象。该对象具有属性,我将其分配给自定义单元格上的标签。这适用于前两个单元格。一旦我滚动足够的第三个单元格创建,该应用程序崩溃。如何解决uitableview中对象的释放问题?

我已经NSZombieEnabled集,这里是输出:

2010-04-19 21:48:13.360 MyApp[54463:207] *** -[CALayer release]: message sent to deallocated instance 0xfc4e50 
(gdb) continue 
2010-04-19 21:48:18.382 MyApp[54463:207] *** NSInvocation: warning: object 0xfc4e50 of class '_NSZombie_CALayer' does not implement methodSignatureForSelector: -- trouble ahead 
2010-04-19 21:48:18.383 MyApp[54463:207] *** NSInvocation: warning: object 0xfc4e50 of class '_NSZombie_CALayer' does not implement doesNotRecognizeSelector: -- abort 
(gdb) continue 
Program received signal: “EXC_BAD_ACCESS”. 
(gdb) 

我不知道什么被释放。我怎样才能追溯到源?

- 编辑 -

添加的cellForRowAtIndexPath执行:

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

static NSString *CellIdentifier = @"Cell"; 

OrderCell *cell = (OrderCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OrderCellView" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

// Set up the cell... 
if(self.rows != nil){ 
    id obj = [self.rows objectAtIndex:indexPath.row]; 
    cell.orderId.text = [NSString stringWithFormat:@"%@",[obj objectForKey:@"OrderId"]]; 
    cell.orderName.text = [NSString stringWithFormat:@"%@",[obj objectForKey:@"OrderName"]]; 
} 

return cell; 
} 
+0

闻起来像是一个由出队单元重新使用引发的问题 - 但对我而言,如果没有看到更多的代码,就无法提供帮助。 – Till 2010-04-20 05:11:22

+0

我已经添加了cellForRowAtIndexPath实现。这还有帮助吗?谢谢。 – 4thSpace 2010-04-20 05:53:32

回答

2

这里是解决方案:

如OP提到的,这是一个自定义单元格并加载笔尖。我正在使用

static NSString *CellIdentifier = @"Cell"; 

但是没有在标识符的IB中指定任何东西。它是空白的。将标识符放入“单元格”后,现在可以正常工作。我猜测笔尖是从代码中创建的单元对象中释放的,因为一个有一个标识符,另一个没有。

我对IB标识符为什么在这种情况下对于单元格如此重要的细节感兴趣...如果任何人能够提供一些细节。

1

您需要阅读的loadNibNamed:owner:options:的文档。您的问题的答案在那里给出:

您应该保留返回的数组或手动包含的对象,以防止提前释放nib文件对象。

我会为load方法返回的数组创建一个实例变量和属性,并确保数组被保留,否则它和它中的所有对象都将在当前事件结束时消失自动释放池被排空。

+0

我创建了@property(nonatomic,retain)NSArray * CellViewNib;并在cellForRowAtIndexPath中使用self.CellViewNib。它没有任何区别。 – 4thSpace 2010-04-20 14:38:52