-1

I am showing more than 10 columns in uitableview cell and having more than 100 rows but when i scroll tableview or reload table it received memory warning and app get crashed. And we are also using collapse and expand on this table view.

Following code applied in cellforrowatindexpath

for(i=0;i<[appDel.tempItemDic count];i++)
{                  
    UILabel *label = [[UILabel  alloc] initWithFrame:CGRectMake(CNT+30, -30, 350,100)];
    label.font = [UIFont systemFontOfSize:14.0];
    label.text =[tempDic objectForKey:[appDel.arrColumnList objectAtIndex:[[appDel.tempArr objectAtIndex:i]intValue]]];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor=[UIColor clearColor];
    label.numberOfLines=1;
    [cell.contentView addSubview:label]; 
    [label release];

    backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ListItems_secondLevel(3).png"]];
    cell.backgroundView = backgroundView;
    cell.imageView.image=[UIImage imageNamed:@"arrow.png"];
    CNT+=350;
}
1
  • That's not how UITable works. Here's a blog post explaining cell reuse.
    – Dan Ray
    Feb 7, 2012 at 13:11

4 Answers 4

0

You need to create the UILabels only while creating the cell. While reusing the cell, you can get the UILabel using tag and set the value to that. Do not create again and again.

Edit: In addition to that, as Sarah mentioned, you don't need to do that in loop. As for every cell the method will get called, and you can set the value depend on the indexPath.

1
  • But,i am creating new object of uilable for each iteration of loop because suppose we have 10 no of columns in tableview than i will have to create 10 new objects of uilabel to show value of each column,so please tell me how to handle this situation.
    – SONOFGOD
    Feb 7, 2012 at 10:41
0

you don't need for loop in the method. As objectatIndex works for the same. remove the for loop as you are already using objectAtIndex and try again.

2
  • @SONOFGOD : you are creating UILabel in the method and that would be reinitialized each time. so you don't need to go for a "for loop"
    – Yama
    Feb 7, 2012 at 10:51
  • But,we are using forloop to show dynamic no of column with their value for each cell. So when cellforrowatindex call above mentioned loop run for no of column to show value of each column.
    – SONOFGOD
    Feb 7, 2012 at 10:55
0

Probably you're not reusing the cell. So if you create 100 cells and each cell has 10 labels at the you will have UILabel views all kept in memory. You should reuse the cell in this way (code not tested, just to give you an idea, I'm not commenting the way you create the text, I'm concerned only about memory warning):


-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  // add your code here to get the number of columns; I assume you defined a "getNumberOfColumnsForRowAtIndexPath:" somewhere in your code to retrieve this figure
  NSInteger numberOfColumns = [appDel getNumberOfColumnsForRowAtIndexPath:indexPath];
  NSString *cellId=[NSString stringWithFormat:@"MyComplexCellID_%d",numberOfColumns];
  UITableViewCell *cell = [tv dequeReusableCellWithIdentifier:cellId];
  if(!cell) {
    // here you create the cell and add the labels; DON'T GIVE TEXT TO THE LABELS HERE
    // in order to get the right label give them a tag; e.g. 500 is column 0, 501 column 1,... 
    for(int i=0;i<numberOfColumns;i++) {
      UILabel *myLabel = ...
      myLabel.tag=500+i;
    }
  }
  // here you configure the cell
  for(int i=0;i<numberOfColumns;i++) {
    NSString *columnText = retrieve here your column text;
    UILabel *labelColumn = (UILabel *)[cell.contentView viewWithTag:500+i];
    labelColumn.text = columnText;
  }

  return cell;

}
3
  • But this is the functionality of my app is that we can add dynamic no of columns,their is no fix no of columns.
    – SONOFGOD
    Feb 7, 2012 at 10:50
  • You can generate a custom ID based on number of columns: 1. get number of columns "num": 2. create cellID: cellId = [NSString stringWithFormat:@"MyCellID_%d",num]; 3. use that cellID You would at least reduce the number of repeating cells. Some more memory taken but not the huge amount you're getting now.
    – viggio24
    Feb 7, 2012 at 10:56
  • see my edited answer; now you get initially the numberOfColumns (I assume you defined a function to do this), then define a specific cellId based on this number (so you will have different type of cells based on the number of columns) and finally you will create the cell.
    – viggio24
    Feb 7, 2012 at 12:31
0

If you are loading a large amount of data in table view I will suggest you

1:-

(void)viewDidLoad {
    [super viewDidLoad];


dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableview reloadData];
});


}

2:-

(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);


dispatch_async(q, ^{

    NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - Main Thread");

    [self methodForServerResponse];
});



}

In that methodForServerResponse-

(void)methodForServerResponse{

  dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);



dispatch_async(q, ^{

    NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - Main Thread within Main Thread");

    //implement what you stuff need from server 
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.