M.B
M.B

Reputation: 885

table view slow scrolling issues

I am using 5 label,1 button,1 image view in custom cells of table view.but when i run my program scrolling of table is slow and also some label are overrided.how can improve performance of scrolling of table my code is as follow Thanks.

 #define NAMELABEL_TAG 1
 #define VALUELABEL_TAG 2
 #define MYPRICELABEL_TAG 3
 #define SAVEPRICELABEL_TAG 4
 #define PRODUCTIMAGELABEL_TAG 5
 #define VLABEL_TAG 6
 #define YLABEL_TAG 7
 #define SLABEL_TAG 8

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


UILabel *nameLabel,*valueLabel,*myPriceLabel,*savePriceLabel,*vLabel,*yLabel,*sLabel;
UIImageView *productImage;
static NSString *CellIdentifier = @"Cell";

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



///my code....

 //CONFIGURE THE SLabel LABEL
 sLabel=[[[UILabel alloc]initWithFrame:CGRectMake(60, 32, 80, 32)]autorelease];
 sLabel.tag=SLABEL_TAG;
 //yLabel.textColor=[UIColor redColor];
 sLabel.font=[UIFont systemFontOfSize:12.0];

 //ADD THE LABEL TO CELLS CONTENT VIEW
 [cell.contentView addSubview:sLabel];


 //CONFIGURE THE YNAME LABEL
 yLabel=[[[UILabel alloc]initWithFrame:CGRectMake(185, 18, 80, 18)]autorelease];
 yLabel.tag=NAMELABEL_TAG;
 //yLabel.textColor=[UIColor redColor];
 yLabel.font=[UIFont systemFontOfSize:12.0];
 //ADD THE LABEL TO CELLS CONTANT VIEW
 [cell.contentView addSubview:yLabel];

 //CONFIGURE THE NAME LABEL
 nameLabel=[[[UILabel alloc]initWithFrame:CGRectMake(60, 0, 140, 20)]autorelease];
 nameLabel.tag=NAMELABEL_TAG;
 nameLabel.textColor=[UIColor redColor];
 //ADD THE LABEL TO CELLS CONTANT VIEW
 [cell.contentView addSubview:nameLabel];
 //configure the value label
 valueLabel=[[[UILabel alloc]initWithFrame:CGRectMake(112, 18, 60, 18)]autorelease];
 valueLabel.tag=VALUELABEL_TAG;
 valueLabel.font=[UIFont systemFontOfSize:12.0];
    //valueLabel.font=[UIFont fontWithName:@"verdana" size:12.0];
    //valueLabel.text=[UIFont 
 valueLabel.textColor=[UIColor redColor];
 [cell.contentView addSubview:valueLabel];

 //CONFIGURE THE myprice LABEL
 myPriceLabel=[[[UILabel alloc]initWithFrame:CGRectMake(262, 18, 40, 18)]autorelease];
 myPriceLabel.tag=MYPRICELABEL_TAG;
 myPriceLabel.font=[UIFont systemFontOfSize:12.0];
 myPriceLabel.textColor=[UIColor redColor];
 //ADD THE LABEL TO CELLS CONTACT VIEW
 [cell.contentView addSubview:myPriceLabel];

 //CONFIGURE THE saveprice LABEL
 savePriceLabel=[[[UILabel alloc]initWithFrame:CGRectMake(135, 32, 50, 32)]autorelease];
 savePriceLabel.tag=SAVEPRICELABEL_TAG;
 savePriceLabel.font=[UIFont systemFontOfSize:12.0];
 savePriceLabel.textColor=[UIColor redColor];
 //ADD THE LABEL TO CELLS CONTACT VIEW
 [cell.contentView addSubview:savePriceLabel];

 // Configure the product Image
 productImage = [[[UIImageView alloc]
 initWithFrame:CGRectMake(0.0, 0.0, 50.0, 60.0)]
 autorelease];
 productImage.tag = PRODUCTIMAGELABEL_TAG;
 // Add the Image to the cell’s content view
 [cell.contentView addSubview:productImage];
 //CONFIGURE THE VNAME LABEL
 vLabel=[[[UILabel alloc]initWithFrame:CGRectMake(60, 18, 50, 18)]autorelease];
 vLabel.tag=VLABEL_TAG;
 vLabel.font=[UIFont systemFontOfSize:12.0];
 //ADD THE LABEL TO CELLS CONTACT VIEW
 [cell.contentView addSubview:vLabel];
 //add button in table view cell...

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0,0,50,60);
    [button setTag:indexPath.row+1];
    //[button setImage:[UIImage imageNamed:@"m1.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:button];


 }

 else {
 nameLabel=(UILabel*)[cell.contentView viewWithTag:NAMELABEL_TAG];
 valueLabel=(UILabel*)[cell.contentView viewWithTag:VALUELABEL_TAG];
 myPriceLabel=(UILabel*)[cell.contentView viewWithTag:MYPRICELABEL_TAG];
 savePriceLabel=(UILabel*)[cell.contentView viewWithTag:SAVEPRICELABEL_TAG];
 productImage = (UIImageView *)[cell.contentView viewWithTag:PRODUCTIMAGELABEL_TAG];
 vLabel=(UILabel*)[cell.contentView viewWithTag:VLABEL_TAG];
 yLabel=(UILabel*)[cell.contentView viewWithTag:YLABEL_TAG];
 sLabel=(UILabel*)[cell.contentView viewWithTag:SLABEL_TAG];
 }


 // Configure the cell...
 //mohit code start

 Product *pro = [self.products objectAtIndex:[indexPath row]];
 nameLabel.text = pro.name;
 valueLabel.text=[[NSNumber numberWithDouble:pro.value]stringValue];
 myPriceLabel.text=[[NSNumber numberWithDouble:pro.price]stringValue];
 savePriceLabel.text=[[NSNumber numberWithDouble:(pro.value-pro.price)]stringValue];
 vLabel.text=@"value Rs:";
 yLabel.text=@"your Price Rs:";
 sLabel.text=@"you Save Rs:";
 NSString *filePath = [[NSBundle mainBundle] pathForResource:pro.image
 ofType:@"png"];
 UIImage *image = [UIImage imageWithContentsOfFile:filePath];
 productImage.image = image;
 //cell.detailTextLabel.text=pro.value;
 /* 
 NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.image ofType:@"png"];
 UIImage *image=[UIImage imageWithContentsOfFile:filepath];
 cell.imageView.image=image;
 */ 
//mohit code finish

return cell;
}

Upvotes: 1

Views: 889

Answers (4)

bmontalvo
bmontalvo

Reputation: 19

I would agree that creating the cell in a nib is the way to go, but if you insist on creating the tableViewCells programatically, here are my suggestions to speed things up: Your vLabel, yLabel, and sLabel elements appear to be static and therefore only need to be set when the cell is first created (inside the 'if (cell == nil)' section).

Also, it appears that your productImage is merely serving as a background for your custom button because their frames overlap. I would set the background image of the custom button to get the same effect. Additionally, the product's price and value are being converted to NSNumber everytime the cell needs to display, perhaps if you stored them as NSNumbers in your Product class, that may help speed things up. Lastly, it looks like you are calculating the savePriceLabel from the product value and price everytime. If those values are unlikely to change, it may be advantageous to calculate the savePrice once when the Product is first created.

I hope these suggestions help.

Also, it looks like you are setting the tag for your button to the indexPath when the cell is first created, but when it gets reused the tag is never updated. That would make the tag value inaccurate if you are using it to keep track of which cell's button was pressed.

Upvotes: 1

Cliff
Cliff

Reputation: 11238

I would look for processor intensive work happening elsewhere in your code since this much you have here looks straight forward. Look in other classes to see if you are doing expensive work on the main thread as that is the most likely source of slow scrolling. Do you have another view loaded perhaps? Is there some networking or database querying going on the main thread?

Upvotes: 1

changx
changx

Reputation: 1957

  1. if vLabel/yLabel/sLabel are constants, assign it while the cell is creating.
  2. convert double to string with [NSString stringWithFormat:@"%.2f", double]
  3. if the images were in your bundle, try [UIImage imageNamed:@"path/to/your/image"], it gets system image cache to reduce the latency of images loading.

Upvotes: 1

carbonbasednerd
carbonbasednerd

Reputation: 130

You could try just making one nib for the content view and setting up the layout for the cell that way. Then you just create the view and assign values. Some of your overlap must just be that the frames you created for the subviews are off. (with a nib you can see that and drag things around). But I'm not sure a nib would help performance. It's just a personal preference to use nibs when I can.

As for speed, you aren't doing anything that looks too processor intensive. What device is this running on or is it the simulator that is running slow? How big is the image you are loading? It looks like you are using the reusable cell properly but have you gone in with the debugger and checked to make sure it wasn't trying to recreate every cell each time? If it is, that could be a possible source or your performance issues.

Upvotes: 1

Related Questions