【发布时间】:2013-12-14 03:31:06
【问题描述】:
我在 xib 文件中有两个 tableviewcell,我都使用它们。
当我使用其中一个时,内存仍然是 7.3MB,但是当我同时使用两个时,内存增长得非常快。
请帮助我。我现在不知道出了什么问题。
我的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
static NSString *CellIdentifierL = @"LeftCell";
static NSString *CellIdentifierR = @"RightCell";
SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];
if (cellLeft == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellLeft = (SampleTableCell *)currentObject;
break;
}
}
}
if (cellRight == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellRight = (SampleTableCell *)currentObject;
break;
}
}
}
return (row%2==0)?cellRight:cellLeft;
}
【问题讨论】:
-
您是否检查了您的内存崩溃报告或仪器日志,因为您的代码看起来不错,您正在使用单元格做什么,我猜您还剩下一些
-
我认为这段代码没有问题。内存占用何时增长?滚动表格视图时?
-
两种理论: 1- 你确定你的
SampleTableCell有正确的reusableCellIdentifier 吗? (在你的 xib 中检查)如果没有,这意味着你创建了太多的行。 2-在创建它们之前,您应该确定必须创建哪个单元格。不过,这只对第一个单元格创建很重要。 -
在使用 2 个单元后,您是否在代码中启用了 Zoombies?
-
哈!那么你的问题在于强引用: property (nonatomic, strong) IBOutlet UILabel *labelDate;这些 IBOutlet 应该是弱的:property (nonatomic, weak) IBOutlet UILabel *labelDate;
标签: ios objective-c memory-management uitableview