tableview中的内存管理结构

时间:2014-03-18 14:11:26

标签: ios iphone

我面临内存问题,目前我正在开发一个杂志应用程序,在该应用程序中我想显示文章列表。我正在获取服务器数据并在表视图中显示内容。我以编程方式在该单元格中创建自定义单元格,我创建了3个标签和一个imageview。但是当我在模拟器中运行这个应用程序时,它们显示的是60 Mb的内存。我很困惑如何解决记忆。

以下是我实施的样本

if (cell == nil)
{

    float cellHeight =[self managingHeight:indexPath.row withSection:indexPath.section];
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor clearColor];

    UILabel *LBL_MainTitle = [[UILabel alloc] initWithFrame:CGRectMake(LEFT_SPACE, TOP_SPACE + CATEGORY_MAX_HEIGHT + (STND_SPACE*2), maximumLabelSize.width - STND_SPACE, 20)];
    [LBL_MainTitle setBackgroundColor:STAT_titleBackgroundClolor];
    [LBL_MainTitle setTextColor:STAT_titleTextColor];
    [LBL_MainTitle setFont:[UIFont fontWithName:STAT_titleFontName size:STAT_titleFontSize]];
    LBL_MainTitle.numberOfLines=0;
    LBL_MainTitle.lineBreakMode=NSLineBreakByWordWrapping;
    [LBL_MainTitle setTag:2];
    LBL_MainTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:LBL_MainTitle];
}

在这个项目中,我启用了ARC。

请检查此信息并告知我们。感谢

2 个答案:

答案 0 :(得分:0)

您应该通过以下方法重复使用单元格。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

以下面的方式编写代码:

static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
    float cellHeight =[self managingHeight:indexPath.row withSection:indexPath.section];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor clearColor];

    UILabel *LBL_MainTitle = [[UILabel alloc] initWithFrame:CGRectMake(LEFT_SPACE, TOP_SPACE + CATEGORY_MAX_HEIGHT + (STND_SPACE*2), maximumLabelSize.width - STND_SPACE, 20)];
    [LBL_MainTitle setBackgroundColor:STAT_titleBackgroundClolor];
    [LBL_MainTitle setTextColor:STAT_titleTextColor];
    [LBL_MainTitle setFont:[UIFont fontWithName:STAT_titleFontName size:STAT_titleFontSize]];
    LBL_MainTitle.numberOfLines=0;
    LBL_MainTitle.lineBreakMode=NSLineBreakByWordWrapping;
    [LBL_MainTitle setTag:2];
    LBL_MainTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:LBL_MainTitle];
}

答案 1 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    float cellHeight =[self managingHeight:indexPath.row withSection:indexPath.section];        
    cell.contentView.backgroundColor = [UIColor clearColor];

    UILabel *LBL_MainTitle = [[UILabel alloc] initWithFrame:CGRectMake(LEFT_SPACE, TOP_SPACE + CATEGORY_MAX_HEIGHT + (STND_SPACE*2), maximumLabelSize.width - STND_SPACE, 20)];
    [LBL_MainTitle setBackgroundColor:STAT_titleBackgroundClolor];
    [LBL_MainTitle setTextColor:STAT_titleTextColor];
    [LBL_MainTitle setFont:[UIFont fontWithName:STAT_titleFontName size:STAT_titleFontSize]];
    LBL_MainTitle.numberOfLines=0;
    LBL_MainTitle.lineBreakMode=NSLineBreakByWordWrapping;
    [LBL_MainTitle setTag:2];
    LBL_MainTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:LBL_MainTitle];
    return cell;
}