【问题标题】:Live bytes growing at cellForRowAtIndexPath:在 cellForRowAtIndexPath 处增长的活动字节:
【发布时间】:2013-10-11 23:53:06
【问题描述】:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TRSocialCell *cell = (TRSocialCell *)[self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TRSocialCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        __weak TREvent *eventFromParse;
        if (!isSearchingEvents){
            if ( [filteredArray[indexPath.section] count] == 0) {
                [cell displayForNoEvents];
                cell.selectedBackgroundView = bgCell;
                return cell;
            } else {
                eventFromParse = filteredArray[indexPath.section][indexPath.row];
            }
        }
        else eventFromParse = searched[indexPath.row];

        //Cover Image
        [eventFromParse.fileForCover getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (!error) {
                eventFromParse.coverPicture = [UIImage imageWithData:data];
                cell.coverView.image = [UIImage imageWithData:data];

            } else {
                [TRAppDelegate displayInternetErrorForView:self.view];
            }

        }];

        cell.titleAutoLabel.text = [NSString stringWithFormat:@"%@",eventFromParse.title];
        cell.titleAutoLabel.fadeLength = 0;
        cell.titleAutoLabel.pauseInterval = 2.0f;
        [animatedLabels addObject:cell.titleAutoLabel];

        if (eventFromParse.location != nil) {
            cell.addressLabel.text = [NSString stringWithFormat:@"%@",eventFromParse.location.name];
        }

        [cell.titleAutoLabel setFont:[UIFont fontWithName:@"BigNoodleTitling" size:26]];
        [cell.titleAutoLabel setTextColor:[UIColor whiteColor]];
        [cell.addressLabel setFont:[UIFont fontWithName:@"BigNoodleTitling" size:18]];
        [cell.addressLabel setTextColor:[UIColor whiteColor]];
        [cell.dateLabel setFont:[UIFont fontWithName:@"BigNoodleTitling" size:18]];
        [cell.dateLabel setTextColor:[UIColor whiteColor]];

        cell.dateLabel.text = [NSString stringWithFormat:@"%@ - %@",[TRAppDelegate convertDateToDate:eventFromParse.date],[TRAppDelegate convertDateToTime:eventFromParse.date]];
        cell.selectedBackgroundView = bgCell;
    }

    return cell;
}

我遇到了麻烦,因为内存在增长,但我找不到是什么。每次滚动到另一个单元格时, if (!cell) {} keep beign 调用是否正常?

我必须在视图控制器的 dealloc 中将属性设置为 nil 吗?

这段代码可能泄露了什么?

【问题讨论】:

  • 简短回答:不,它应该只在屏幕上显示前几个单元格时调用,然后它应该重用一个预先存在的单元格。顺便说一句,这是在单元格上设置数据的正确模式(您使用的是旧方式......新方式使用原型单元格,它取消了if (cell == nil) 检查)gist.github.com/iwasrobbed/1656ddf1b0ca645bd123

标签: ios objective-c uitableview memory-management memory-leaks


【解决方案1】:

我遇到了麻烦,因为内存在增长,但我找不到是什么。每次滚动到另一个单元格时都会调用 if (!cell) {} keep beign 是否正常?

不。您没有将新分配的单元格放入重用队列中。因此,每次您需要提供一个新单元时,您基本上都是在将一个新笔尖加载到 RAM 中。你这样做的方式,新的TRSocialCells 不知道它们的重用标识符是什么 - 因此它们显然根本不会被重用。

尝试在您的TRSocialCell 实现文件中实现以下方法,以便您的单元格在系统询问时可以返回它们的reuseIdentifier

- (NSString *)reuseIdentifier {
   return @"cell";
}

顺便说一句:
对于reuseIdentifier,'cell' 并不是最聪明的选择。如果有一天你想使用不同类型的单元格,它真的会弄乱你的重用队列。建议您在标识符中包含类名。例如TRSocialCellID

【讨论】:

  • 谢谢!我更改了重用标识符并删除了原型单元!还有 BAM!有效。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 2019-11-08
相关资源
最近更新 更多