【问题标题】:UITableView elements cached缓存的 UITableView 元素
【发布时间】:2011-11-18 18:07:51
【问题描述】:

我有一个包含 1000 个元素的 UITableView。在第一次加载它之前 Instruments 显示 2.20MB 的字节仍然存在。充电后显示4.82MB。当我释放内存并返回之前的状态时,它显示为 4.71MB。但是现在当我加载同一张表时,仪器再次显示 4.82MB。 UITableView 的结构是否存储在缓存中?如果是,有什么办法可以释放这段内存?

我的表结构:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"SearchResult";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:MyIdentifier] autorelease];
    } 

    if(!isSomeStateSearching) cell.textLabel.text = [[[contentSomeState objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row];
    else
    {
        cell.textLabel.text = [searchedAllContent objectAtIndex:indexPath.row];
    }

    UIView *v = [[[UIView alloc] init] autorelease];
    v.backgroundColor = [UIColor colorWithRed:188/255.0 green:57/255.0 blue:25/255.0    alpha:1.0];
    cell.selectedBackgroundView = v;

    UIImageView *arrow = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seta_navegacao"]] autorelease];
    cell.accessoryView = arrow;

    return cell;
}

【问题讨论】:

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


    【解决方案1】:

    你当然正确地使用了UITableViewCell的重用机制,对吧?

    【讨论】:

    • 是的,我会补充问题。
    【解决方案2】:

    您应该仅在初始化新单元格时分配和添加单元格子视图。就目前而言,每次重用单元格时,您都会创建一个新的视图和图像视图。这就是为什么你的表占用了这么多内存的原因。

    【讨论】:

    • 问题是没有占用这么多内存。问题是内存最终没有释放。我可以在代码末尾注释视图和图像视图,问题仍然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多