【发布时间】:2014-03-06 13:56:35
【问题描述】:
我正在处理一个没有 ARC 的旧项目。它有很多错误,代码看起来很丑,我正在重写它。
快速查看我的代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell = [self createCellWithInfo:[self.search objectAtIndex:indexPath.row]];
return cell;
}
-(UITableViewCell *)createCellWithInfo:(NSDictionary *)info{
UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“Cell”] autorelease];
//set image for cell
//set text for cell.textlabel
//set text for cell.detailTextLabel
//create an UIButton and add to cell.content view
return cell;
}
重点在这行代码
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“Cell”] autorelease]
如果我在这里使用@"Cell",那么当我在桌子上连续上下滚动时,内存会上升。
滚动大约 15 秒后,我的 iphone 5c 变得滞后。
如果我将它设置为nil,一切都很好。
有人可以解释一下吗?我不熟悉非ARC。
谢谢。
【问题讨论】:
-
在 Objective C 中评估的一个好习惯是:
if (!cell)而不是if (cell == nil) -
除非有充分的理由不点击“编辑 -> 转换为 Obj-C ARC”?
-
我同意@MikePollard,我敢打赌这个应用比爱德华·斯诺登泄漏更多。
-
大声笑,这是一个非常生动的比较。回到主题,如果我可以将其转换为 ARC,我会这样做,但它包含一些不支持 ARC 的旧添加类,例如
ASIHTTPRequest。我添加了-fnc -objc -arc,但出现了一些其他错误,我正在处理它。同时,这个项目还要继续推进,你知道的......
标签: ios objective-c uitableview automatic-ref-counting