UITableView在滚动时崩溃

Mag*_*ave 5 iphone objective-c uitableview

我有一个构建和绘制确定的TableView,但随后在滚动视图时崩溃.我已经浏览了调试器,似乎我的类级变量被某种方式覆盖,因此当再次调用titleForHeaderInSection时它们不再存在.非常奇怪的是,如果我替换代码:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [favouritesDataSections objectAtIndex:section];
return sectionTitle;
}
Run Code Online (Sandbox Code Playgroud)

有:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = @"Test";
return sectionTitle;
}
Run Code Online (Sandbox Code Playgroud)

它仍然崩溃,但这次当你将鼠标悬停在sectionTitle变量上时,调试器不会列出NSString.

这是我用来创建视图和设置类级变量的代码:

- (void)loadView {
[super loadView];
CGRect tableSize = CGRectMake(0,0,320,460);
UITableView *favouritesTableView = [[UITableView alloc] initWithFrame:tableSize style:UITableViewStylePlain];
favouritesTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
favouritesTableView.dataSource = self;
favouritesTableView.delegate = self;
favouritesTableView.rowHeight = 52;
[self.view addSubview:favouritesTableView];
}

- (void)viewDidLoad {
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Get the full path of the favourites plist
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];
// Initialise Dictionary and array
favouritesDataAll = [[NSMutableDictionary alloc] init];
favouritesDataSections = [[NSArray alloc] init];

NSDictionary *dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:filename] retain];
favouritesDataAll = dict;
[dict release];

favouritesDataSections = [favouritesDataAll allKeys];   
}
Run Code Online (Sandbox Code Playgroud)

我绝对会疯狂地试图追踪这一点 - 到目前为止花了2天时间,所以在外面感激任何帮助.

最好的祝福

戴夫

Mag*_*ave 12

好的,修好了......改了

favouritesDataSections = [favouritesDataAll allKeys];
Run Code Online (Sandbox Code Playgroud)

至:

favouritesDataSections = [[favouritesDataAll allKeys] retain];
Run Code Online (Sandbox Code Playgroud)

这一切似乎都有效.从这里我可以推断出我用来存储章节标题的数组指的是在某个随机点自动释放的数据,这就是为什么它在看似奇怪的地方bar.

我承认,虽然我仍处于编码的"试错"阶段,并且不完全理解我在做什么(我相信你会读到这个).如果您有任何想法/评论链接进一步阅读或有关所有这些如何工作的帖子(即何时以及为何使用保留等)以促进我的理解,那将对我有用.

再次感谢,戴夫