2011-06-11 59 views
1

我有一张表格,其内容使用根据附加的分段控件的值提供的两个数据集中的一个填充。数据从网络中提取。分段UITable的不稳定性

当我在两个数据集之间来回移动时,应用程序崩溃,然后选择一个表格单元格。当我使用两个单独的表格而不是带有分段控件的单个表格时,该应用程序不会打嗝。以某种方式组合它们会使应用程序不稳定。

其中一个最大的错误是当我点击一个Acell的Acells并且它的加载时,我点击了segment index = 1,这会加载B.它会导致无法识别的选择器被发送。参数的含义o要发送到B表的表。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier0; 
    if (choice == 0) { 
     CellIdentifier0 = @"ACell"; 
    } else if (choice == 1) { 
     CellIdentifier0 = @"BCell"; 
    } 

    UITableViewCell *cell; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0]; 
    if (cell == nil) { 
     if (choice == 0) { 
      [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil]; 
     cell = ACell; 
      self.ACell = nil; 
     } else if (choice == 1) { 
      [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil]; 
      cell = BCell; 
      self.BCell = nil; 
     } 
    } 

    if (choice == 0) { 
     [(ACell *)cell setA:(A*)[contentArray objectAtIndex:indexPath.row]]; 
    } else if (choice == 1) { 
     [(BCell *)cell setB:(B*)[contentArray objectAtIndex:indexPath.row]]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (choice == 0) { 
     A = [contentArray objectAtIndex:indexPath.row]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showA" object:A]; 
    } else if (choice ==1) { 
     B = [contentArray objectAtIndex:indexPath.row]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showB" object:B]; 
    } 
} 
+2

这可能是重复使用单元格的问题。没有**代码的**没有办法说。 – PengOne 2011-06-11 01:55:18

+0

下面是代码 – lilzz 2011-06-11 04:38:00

回答

0

假设ACell具有保留价值加载笔尖文件时,做self.ACell = nil;将释放旧的价值,并设置ACellnil。现在较旧的值是cell指向的值。所以在技术上它会指向垃圾,因为它将被释放,因为ACell是保留该对象的唯一变量。您可以通过更换解决这个问题,

cell = ACell; 

用,

cell = [[ACell retain] autorelease]; 

一点题外话,在didSelectRowAtIndexPath:方法你做} else if (choice = 1) {。请确保您使用的是==而不是=,它将始终与更改choice的值一起评估为真,但在此情况下不太可能。

+0

,保留没有帮助。 – lilzz 2011-06-11 18:28:25

+0

@ user482473你可以添加'didSelectRowForIndexPath:'方法吗? – 2011-06-11 18:39:32

+0

当然,我可以选择表A和表B中的单个单元格(Acell或Bcell)。 – lilzz 2011-06-11 19:29:21

0

检查拼写选择

if (choicee==0) { 
[[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil]; 
    cell = ACell; 
    self.ACell = nil;} 
else if (choice==1) {