pengwang
pengwang

Reputation: 19956

UiTableView load many cell and MemoryWarning

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

    PostCount *post=[listArr objectAtIndex:indexPath.row];

    //NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%d_%d_%@_%d",indexPath.section,indexPath.row,post.foreignId,[listArr count]];
    NSString *CellIdentifier = [NSString stringWithFormat: @"Cell_%d_%@",indexPath.row,post.foreignId];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

        NSLog(@"indexPath.row++++++++=%d",indexPath.row);

        TimeLineGraphicView *gview=[[TimeLineGraphicView alloc]init];
        gview.tag=indexPath.row+1000;
        gview.delegate=self;

        [cell addSubview:gview];

        int Allheight =[ModelClass returnGraphicViewHeight_timeLine:post];
        gview.frame=CGRectMake(0, 0, 320, Allheight);
        [gview setViewStyle:post];
    }

    TimeLineGraphicView *gview=(TimeLineGraphicView *)[cell viewWithTag:indexPath.row+1000];
    gview.lab_time.text=[ModelClass intervalSinceNow:post.when btime:0];

    //NSLog(@"intervalSinceNow=%@  ",[ModelClass intervalSinceNow:post.when btime:0]);
    //NSLog(@"post.when=%@  gview=%@  gview.lab_time.text=%@",post.when,gview, gview.lab_time.text);   

    return cell;

}

hello,if i use the above code if i have many cell, TimeLineGraphicView *gview=[[TimeLineGraphicView alloc]init] can increase memory,because when i load many cell for example first i load 15 cell,then add 15 cell then add 15 cell and so on,it give me didReceiveMemoryWarning,can you good practice to deal with the problem

Upvotes: 0

Views: 95

Answers (2)

akshaynhegde
akshaynhegde

Reputation: 1938

The way you are implementing the things are not right.

  • Why don't you subclass UITableViewCell ? Let's say, in your case why not a "TimelineGraphicTableviewCell" ?
  • Adding your "TimeLineGraphicView" as a subView to the contentView of the cell can be done there.
  • Set the frame or such of the subView, in layoutSubviews of the custom class.
  • In – cellForRowAtIndexPath: you can just create it and set the data.!

I think if you implement things in the proper way, tableview should not show any memory warning no matter how many lines there are..!

If you are still unclear on customizing tableViewCells, just google it, you will find tons of tutorials.

Good luck..!

Upvotes: 1

B.S.
B.S.

Reputation: 21726

You do not release the cell

 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

You do not release the view

[cell addSubview:gview];
[gview release];

Upvotes: 1

Related Questions