【发布时间】:2015-12-21 13:41:47
【问题描述】:
当我滚动 UITableView 时,内存不断增加,当我运行“Allocations”时,我得到了这种结果。
我看到他们的 VM:UITableViewLabel 内存不断增加并且是持久的,他们有什么办法可以消除这种不断增加的内存
-(UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString *text = @"";
BOOL defaultStyle = YES;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (defaultCell == nil) {
defaultCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else{
NSLog(@"reusing the cell");
}
defaultCell.textLabel.textAlignment = NSTextAlignmentLeft;
defaultCell.userInteractionEnabled = YES;
defaultCell.textLabel.font = [UIFont systemFontOfSize:26.0f];
UIImageView *iv = defaultCell.imageView;
int totalSections = [self numberOfSectionsInTableView:tableView];
NSInteger computedSection = indexPath.section;
if (defaultStyle)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.backgroundColor = [UIColor whiteColor];
defaultCell.textLabel.textColor = [UIColor blackColor];
}
else if (computedSection == 0)
{
const int totalRows = [self tableView:self.tableView numberOfRowsInSection:indexPath.section];
if (indexPath.row == 0) {
text = @"Style1";
defaultStyle = NO;
if (self.style1 == nil)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
}
else
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor blackColor];
}
iv.image = [UIImage imageNamed: @"Style1.png"];
}
if(indexPath.row == totalRows - 2){
// Categories
text = @"Style2";
iv.image = [UIImage imageNamed: @"Style2.png"];
defaultStyle = NO;
if ( self.style2 == nil) {
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
} else {
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor blackColor];
}
}
else if (indexPath.row == totalRows - 1){
// Search
text = @"Style3";
iv.image = [UIImage imageNamed: @"Style3.png"];
}
}
else if (computedSection == 1)
{
MyCustomTableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:kCustomTableCellIdentifier];
if ( cell == nil ) {
cell = [CustomTableViewCell loadFromNibNamed:@"CustomTableViewCell"];
}
cell.titleLabel = @"custom cell";
cell.openImage =[UIImage imageNamed: @"custom.png"]
return cell;
}
else if (indexPath.section == totalSections - 1)
{
if (indexPath.row == 0)
{
text = @"Account";
defaultStyle = NO;
if (self.hasAccount == nil)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
}
else
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor whiteColor];
}
iv.image = [UIImage imageNamed: @"Account.png"];
}
}
defaultCell.textLabel.text = text;
return defaultCell;
}
【问题讨论】:
-
能否也显示
ProgrammeTableViewCell类的标头及其dealloc方法的实现? -
您用来加载
ProgrammeTableViewCell的loadFromNibNamed:方法是什么?如果它只是loadNibNamed:owner:options:的通用包装器,它可能是为 ARC 环境编写的,而您使用的是非 ARC,因此您可以轻松地在其中搞乱自动释放。还有registerNib:forCellReuseIdentifier:和dequeueReusableCellWithIdentifier:forIndexPath:,如果需要,它们会自动从NIB 创建单元格,并消除if (cell == nil)检查的负担。嘿,为什么根本不是 ARC?
标签: ios memory-management instruments