2011-11-25 72 views
0

我在这里撕掉我的头发。我的RootViewController从plist中加载一个字符串数组。当它加载它工作正常。然后点击将其发送到新视图控制器的行。如果我回去,桌子仍然正常工作,所以我点击另一个按预期工作的行。现在,如果我回去,我滚动在cell.textLabel.text = [self.faceCategories objectAtIndex:indexPath.row];用EXC_BAD_ACCESS和UITableViewCell挣扎

摘编源代码如下得到一个崩溃指点:

// RootViewController.h 

#import <UIKit/UIKit.h> 

@class DetailViewController; 

@interface RootViewController : UITableViewController { 
    NSMutableArray *faceCategories; 
} 
@property (nonatomic, retain) NSMutableArray *faceCategories; 
@end 

RootViewController的实现 // RootViewController.m

#import "RootViewController.h" 

@synthesize faceCategories; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Init array 
    if (self.faceCategories == nil) { 
     NSLog(@"NIL NIL NIL NIL NIL NIL NIL NIL NIL"); 
     NSError *error; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //Create a list of paths. 
     NSString *documentsDirectory = [paths objectAtIndex:0]; //Get a path to your documents directory from the list. 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"faceCategories.plist"]; //Create a full file path. 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     if (![fileManager fileExistsAtPath: path]) //Check if file exists. 

     { 
      NSString *bundle = [[NSBundle mainBundle] pathForResource:@"faceCategories" ofType:@"plist"]; //Get a path to your plist created before in bundle directory (by Xcode). 
      [fileManager copyItemAtPath:bundle toPath: path error:&error]; //Copy this plist to your documents directory. 
     } 
     // Zombies percentages after 
     NSMutableArray *fileContents = [[NSMutableArray alloc] initWithContentsOfFile: path]; // 55.6% 
     self.faceCategories = [[NSMutableArray alloc] initWithArray:fileContents copyItems:YES]; // 33.3% 
     [fileContents release]; // 11.1% 

    } 
} 

// Customize the appearance of table view 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]; 
    } 

    ///////////////////////////// 
    // CRASH HERE only after I return to this view controller a couple of times 
    // If I enable breakpoints and add a log I can see that faceCategories still contains the right number of objects 
    ///////////////////////////// 
    cell.textLabel.text = [self.faceCategories objectAtIndex:indexPath.row]; 

    return cell; 
} 


- (void)dealloc 
{ 
    [faceCategories release]; 
    [super dealloc]; 
} 

@end 

虽然我的内存管理能力是基本的,我看不出有什么特别的错误,所以如果有人愿意,我会欣赏一个新的眼睛?我觉得特别奇怪的是,当我回到根视图控制器几次时,它才会显现出来。

完整的源 - http://pastebin.com/5XzwN0bA

+0

你的代码有点格式化(例如在代码块的中间释放)。你能清理吗?另外,你做'if(self.faceCategories == nil)'检查其他地方,为什么不在你的'cellForRowAtIndexPath'方法中做同样的检查? –

+0

我的错误 - 当我复制和编辑文本时发生。 – squarefrog

+0

我曾尝试添加一个if,但它通过条件很好,然后在设置单元格文本时失败。 – squarefrog

回答

0

虽然不完全是一个解决方法,我发现,要摆脱这些错误的最简单的方法是使弧。要做到这一点去编辑>重构>转换为Objective-C ARC。

阅读了各种ARC博客文章,但这样做意味着我完全忘记了内存管理,并专注于新功能。

1

尝试之后修改三行:// Zombies percentages after 有:

​​3210
+0

我试过了,最终结果是一样的。 – squarefrog

+0

上面的来源没有任何问题。但是你并没有粘贴你的所有代码。发布这个类的其余部分,并告诉我们你在与这个视图控制器有关的xib文件中做了什么连接。 – joerick

+0

您可能会保留faceCategories数组,在其他地方粘贴整个rootviewcontroller代码以帮助您找到问题。 – Mousa