Memory leaks 启用ARC的自定义单元内存泄漏
为什么启用ARC时内存泄漏(以粗体突出显示) 我有手机Memory leaks 启用ARC的自定义单元内存泄漏,memory-leaks,uitableview,automatic-ref-counting,custom-cell,Memory Leaks,Uitableview,Automatic Ref Counting,Custom Cell,为什么启用ARC时内存泄漏(以粗体突出显示) 我有手机 +(CustomCell*)cell { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPhone" owner:self options:nil];
+(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**
...
}
所以,有两件事。第一,我想你正在一个.xib文件中创建这个单元格。在IB中的单元格上设置重用标识符。然后,在tableView:cellForRowAtIndexPath:中卸载nib,而不是此CustomCell类方法,如下所示:
- (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;
}
第二件事是,通过尝试先将可重用单元出列,您将获得更好的性能