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;
}
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?