缓存了UITableView元素

时间:2011-09-16 14:22:30

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

我有一个包含1000个元素的UITableView。在第一次加载它之前,仪器显示2.20MB的字节仍然存活。充电后显示4.82MB。当我释放内存并返回到先前的状态时,它显示4.71MB。但现在当我加载相同的表时,Instruments再次显示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;
}

2 个答案:

答案 0 :(得分:0)

当然,您正确使用UITableViewCell的重用机制,对吗?

答案 1 :(得分:0)

只有在初始化新单元格时才应分配和添加单元子视图。目前,您每次重复使用单元格时都会创建一个新的视图和图像视图。这就是为什么你的桌子占用了这么多内存。