为什么启用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 conteroller中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell=[CustomCell cell]; **// 100% leaking**
...
}
答案 0 :(得分: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;
}
第二件事是,通过首先尝试将可重复使用的单元格出列,您将获得更好的性能。