khe*_*aud 2 memory iphone memory-management uitableview
我正在改变UITableViewCellStyleSubtitle的背景,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[...]
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"bgCellNormal" ofType:@"png"];
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]] autorelease];
[...]
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来做到这一点,而不使用这么多的alloc和autorelease?我的观点是在这些uitableview中优化内存!
谢谢你的帮助 !
Kheraud
您不应该访问或设置该backgroundView属性tableView:cellForRowAtIndexPath:.框架可能还没有实例化它,它可能会在你的脚下取而代之.在这方面,分组和普通表视图的行为不同,因此任何新的未来风格也可能不同.
应在中设置和/自定义背景视图tableView:willDisplayCell:forRowAtIndexPath:.在第一次显示调用之前调用此方法.如果您愿意,可以使用它完全替换背景.我通常做这样的事情:
-(void) tableView:(UITableView*)tableView
willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath;
{
static UIImage* bgImage = nil;
if (bgImage == nil) {
bgImage = [[UIImage imageNamed:@"myimage.png"] retain];
}
cell.backgroundView = [[[UIImageView alloc] initWithImage:bgImage] autorelease];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1799 次 |
| 最近记录: |