我在这里撕掉我的头发。我的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
你的代码有点格式化(例如在代码块的中间释放)。你能清理吗?另外,你做'if(self.faceCategories == nil)'检查其他地方,为什么不在你的'cellForRowAtIndexPath'方法中做同样的检查? –
我的错误 - 当我复制和编辑文本时发生。 – squarefrog
我曾尝试添加一个if,但它通过条件很好,然后在设置单元格文本时失败。 – squarefrog