[英]EXEC_BAD_ACCESS in UITableView cellForRowAtIndexPath
我的UITableView返回EXEC_BAD_ACCESS
,但为什么!
看到这段代码片段!
加载UITableView工作正常,所以allXYZArray != nil
并填充!
然后将tableview滚动到底部并备份导致它崩溃,因为它重新加载方法cellForRowAtIndexPath
它失败了:
"NSLog(@"allXYZArray::count: %i", [allXYZArray count]);"
(UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAt
IndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
@try
{
if (allXYZArray == nil) {
NSLog(@"nil");
allXYZArray = [ToolBox getMergedSortedDictionaries:allXYZGiven SecondDictionary:allXYZSought];
}
NSLog(@"%i", [indexPath row]);
NSLog(@"allXYZArray::count: %i", [allXYZArray count]);
EXC_BAD_ACCESS表示您的程序正在尝试访问无效或无法从您的进程访问的内存地址。 当您尝试向已经解除分配的对象发送消息时,最常发生这种情况。 因此,调试EXC_BAD_ACCESS的第一步是确定程序尝试向崩溃发生时发送消息的对象。 通常答案并不明显,在这种情况下, NSZombieEnabled是一个很好的工具,用于识别导致崩溃的代码行。
在你的情况下,当你调用[allXYZArray count]
时,你已经确定发生了崩溃,使得allXYZArray
成为我们的主要嫌疑人。 这个对象是从+[ToolBox getMergedSortedDictionaries:SecondDictionary:]
,所以很可能你的bug是在该方法的实现中。 我猜它会返回一个已经被释放的对象而不是自动释放的对象,正如Cocoa的内存管理编程指南所规定的那样。 (顺便说一句,这是SDK中最重要的文档之一。我建议每月重读一次,直到其政策和技术成为第二天性。)
好的,重用一个单元格,并不能保证单元格能够正确初始化:
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
单元格有时会为空(特别是我第一次猜)。
检查单元格是否为空,如果是,请正确初始化它。
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.