我有这样的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"BSTGListCell";
BSTGListCell *cell = (BSTGListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"BSTGListCell" owner:self options:nil] objectAtIndex:0];
}
PFObject* currentEl = [self.tableData objectAtIndex:indexPath.row];
cell.title.text = [currentEl objectForKey:@"Name"];
cell.description.text = [currentEl objectForKey:@"Address"];
return cell;
}
当向下滚动作为子视图添加的表视图时,我得到"消息发送到deallocated实例"。僵尸检查器说被访问的对象保留在这里:
cell = [[[NSBundle mainBundle] loadNibNamed:@"BSTGListCell" owner:self options:nil] objectAtIndex:0];
,可能是ARC发布的。为什么会发生这种情况,我该如何预防?
你真的不应该这样做。从nib中使用cell的方法是注册nib,可能在viewDidLoad中,像这样:
UINib *nib = [UINib nibWithNibName:@"BSTGListCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"BSTGListCell"];
然后在cellForRowAtIndexPath中,使用dequeueReusableCellWithIdentifier:forIndexPath:和no if(cell == nil)子句。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BSTGListCell *cell = (BSTGListCell *)[tableView dequeueReusableCellWithIdentifier:@"BSTGListCell" forIndexPath:indexPath];
PFObject* currentEl = [self.tableData objectAtIndex:indexPath.row];
cell.title.text = [currentEl objectForKey:@"Name"];
cell.description.text = [currentEl objectForKey:@"Address"];
return cell;
}
你的代码的实际问题是loadNibNamed:owner:options,返回一个数组,你必须得到一个对象从该数组之前,你分配给单元格。但是,我展示的方法是一种更有效的方法。
我的问题是dequeueReusableCell
行,我把它拿出来,就像这个链接一样,现在我的工作