0

我有这个代码:

 - (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;
}

向下滚动作为子视图添加的表视图时,我收到“发送到已释放实例的消息”。Zombie inspector 说被访问的对象被保留在这里:

cell = [[[NSBundle mainBundle] loadNibNamed:@"BSTGListCell" owner:self options:nil] objectAtIndex:0];

并且可能由 ARC 发布。为什么会发生这种情况,我该如何预防?

4

2 回答 2

1

你真的不应该这样做。从 nib 使用 cell 的方法是注册 nib,可能在 viewDidLoad 中,如下所示:

UINib *nib = [UINib nibWithNibName:@"BSTGListCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"BSTGListCell"];

然后在你的 cellForRowAtIndexPath 中,使用 dequeueReusableCellWithIdentifier:forIndexPath: 并且没有 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;
}

The actual problem with your code is that loadNibNamed:owner:options, returns an array, and you have to get the one object out of that array before you assign it to cell. But, the way I showed is a more efficient way to do this anyway.

于 2013-03-30T16:04:18.397 回答
0

My problem was something with the dequeueReusableCell line, i took it out like this link did and mine works now

于 2013-04-09T20:04:22.087 回答