2010-09-08 102 views
0

在我的程序,当我创建一对夫妇从笔尖文件加载定制UIViewCells的:EXC_BAD_ACCESS滚动的TableView

[[NSBundle mainBundle] loadNibNamed:@"CustomCells" owner:self options:nil]; 

一旦他们加载我设置起来,并从函数返回:

if (indexpath.row == 1) { 
    [nibTextInputer setupWithName:@"notes" ...]; 
    return nibTextInputer; 
} else { 
    [nibSelectInputer setupWithName:@"your_choice" ...]; 
    return nibSelectInputer; 
}; 

其中nibTextInputer是我的班级(AFTextInputer)和nibSelectInputer是我的其他班级(AFTextInputer)。这两个类都来自UITableViewCell的子类。

这一切工作正常,但休息时,我添加缓存到:

Boolean inCache = false; 
if (indexPath.row == 1) { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nibTextInputer"]; 
    if (cell != nil) { 
     NSLog(@"%@", [cell description]); // prints out ok, correct type. 
     nibTextInputer = (AFTextInputer*) cell; 
     inCache = true; 
    }; 
}; 

if (!inCache) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCells" owner:self options:nil]; 
} 

一旦我添加上面EXC_BAD_ACCESS开始出现在随机的地方,通常没有额外的信息,有时与此错误:

-[CALayer prepareForReuse]: unrecognized selector sent to instance 

甚至

-[UIImage prepareForReuse]: unrecognized selector sent to instance 

EXC_BAD_ACCES的位置S看起来是随机的。有时,它的“出列”之后,有时是郊外的功能..

我想这个问题就在于我的实现定制UIViewCells之内,但我不知道从哪里开始寻找..

想法?

回答

2

你有一个过度释放发生在你的UITableViewCell-[UITableViewCell prepareForReuse]在从-[UITableView dequeueReusableCellWithIdentifier:]返回之前被调用,但是当它被调用时,单元不再存在,而是CALayer,UIImage或您无权访问的内容。

问题可能出在您加载自定义单元格的方式上。对于它的价值,这是我通常如何做到这一点:

static NSString *CellIdentifier = @"CustomCell"; // This string should also be set in IB 

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = nibCell; // nibCell is a retained IBOutlet which is wired to the cell in IB 
} 

// Set up the cell... 
0

这可能是你要去的地方碰到的问题:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nibTextInputer"]; 

的的UITableView类肿块所有的细胞进入重复使用相同的池;它不知道某些单元是一种子类(即AFTextInputer),有些单元是另一种子类(即AFTextInputer)。因此,当您在if (indexPath.row == 1)块中取出某个单元格时,您可能会遇到错误的子类型单元格。 “标识符”只是一个字符串,用于向内置高速缓存指示您所引用的表单元格;它实际上并不使用该字符串挖掘缓存以找到具有匹配子类名称的对象。

P.S.为什么使用名为Boolean的类型而不是“内置”BOOL

+0

这就是..CellWithIdentifier:用于 - 标识符(“nibTextInputer”)在nib文件中设置,并且我通过NSLogging单元格类型进行双重检查。 – kolinko 2010-09-08 17:25:10

+0

我不知道有什么区别wetween布尔和布尔..我会研究,谢谢。 – kolinko 2010-09-08 17:25:44