gpt4 book ai didi

iphone - 表格 View 滚动缓慢的问题

转载 作者:行者123 更新时间:2023-12-01 18:31:32 25 4
gpt4 key购买 nike

我在表格 View 的自定义单元格中使用 5 个标签、1 个按钮、1 个 ImageView 。但是当我运行我的程序时,表格的滚动速度很慢,并且一些标签被覆盖。如何提高表格滚动的性能我的代码如下
谢谢。

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

最佳答案

我会在您的代码中的其他地方寻找处理器密集型工作,因为您在这里拥有的这么多看起来很简单。查看其他类,看看您是否在主线程上做昂贵的工作,因为这很可能是滚动缓慢的原因。您是否加载了另一个 View ?主线程上是否有一些网络或数据库查询?

关于iphone - 表格 View 滚动缓慢的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com