0

为什么我在启用 ARC 的情况下会出现内存泄漏(以粗体突出显示)?

我有 CustomCell.m

   +(CustomCell*)cell
{


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPhone" owner:self options:nil];         
        return [nib objectAtIndex:0];

    } else {
        NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPad" owner:self options:nil];          **//leaking 100%**  
        return [nib objectAtIndex:0];

    }
}

在我的 tableview 控制器中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell=[CustomCell cell]; **// 100% leaking**
...
}
4

1 回答 1

1

所以,有两件事。一,我猜你是在 .xib 文件中创建这个单元格。在 IB 中的单元格上设置重用标识符。然后,代替这个 CustomCell 类方法,卸载 tableView:cellForRowAtIndexPath: 中的 nib,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Assuming you set a reuse identifier "cellId" in the nib for your table view cell...
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (!cell) {
        // If you didn't get a valid cell reference back, unload a cell from the nib
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
        for (id obj in nibArray) {
            if ([obj isMemberOfClass:[MyCell class]]) {
                // Assign cell to obj, and add a target action for the checkmark
                cell = (MyCell *)obj;
                break;
            }
        }
     }

     return cell;
}

第二件事是,通过首先尝试使可重用单元出列,您将获得更好的性能。

于 2012-04-03T22:58:26.170 回答