我在这里撕扯我的头发。我的RootViewController从plist加载一个字符串数组。当它加载它工作正常。然后我点击一行将其发送到新的视图控制器。如果我回去,桌子仍然正常,所以我点击另一行按预期工作。现在,如果我回去滚动,我会发现指向cell.textLabel.text = [self.faceCategories objectAtIndex:indexPath.row];
以下摘要源代码:
// 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 :(得分:1)
尝试修改:// Zombies percentages after
之后的三行:
NSMutableArray *fileContents = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.faceCategories = fileContents;
[fileContents release];
答案 1 :(得分:0)
虽然不完全是一个修复,但我发现摆脱这些错误的最简单方法是启用ARC。要执行此操作,请转到编辑&gt;重构&gt;转换为Objective-C ARC。
阅读各种ARC博客文章,但这样做意味着我完全忘记了内存管理,并专注于新功能。