【发布时间】:2012-11-20 17:36:09
【问题描述】:
非常基本的 cellForRowAtIndexPath 实现。
我想知道为什么分析器告诉我我在这里泄漏了内存。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyObject *object = [[self.datasource objectForKey:key] objectAtIndex:indexPath.row];
cell.textLabel.text = object.title;
return cell;
}
分析仪告诉我“细胞”可能会泄漏。这只是分析仪的一个弱点还是我应该如何处理?
注意:像这样添加对 autorelease 的调用:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] autorelease];
导致崩溃。有什么想法吗?
【问题讨论】:
-
在你的
if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }你的 alloc/init 中没有自动释放看看是否有帮助
标签: objective-c ios xcode memory-management