我有一个UITableView,并且我正在向单元格添加自定义视图。它真的很慢,但只有2个细胞。这是它的样子,当上下滚动时,我可以看到它卡顿/滞后如此之多(这是在iPhone 4s上)。我看了看App Store应用程序,非常复杂的tableviewcell滚动非常流畅。我做错了什么吗?
下面是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
int row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UILabel *labelBarcode = [[UILabel alloc] initWithFrame:CGRectMake(80, 7, 150, 15)];
[labelBarcode setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
[labelBarcode setTag:10];
UILabel *labelLength = [[UILabel alloc] initWithFrame:CGRectMake(80, 22, 150, 15)];
[labelLength setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelLength setTag:11];
UILabel *labelThickness = [[UILabel alloc] initWithFrame:CGRectMake(80, 37, 150, 15)];
[labelThickness setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelThickness setTag:12];
UILabel *labelDiameter = [[UILabel alloc] initWithFrame:CGRectMake(190, 22, 100, 15)];
[labelDiameter setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelDiameter setTag:13];
UILabel *labelCoating = [[UILabel alloc] initWithFrame:CGRectMake(190, 37, 100, 15)];
[labelCoating setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelCoating setTag:14];
UIImageView *thumbView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 65, 50)];
[thumbView setContentMode:UIViewContentModeScaleToFill];
[thumbView setTag:15];
[[cell contentView] addSubview:thumbView];
[[cell contentView] addSubview:labelBarcode];
[[cell contentView] addSubview:labelLength];
[[cell contentView] addSubview:labelThickness];
[[cell contentView] addSubview:labelDiameter];
[[cell contentView] addSubview:labelCoating];
[thumbView release];
[labelBarcode release];
[labelLength release];
[labelThickness release];
[labelDiameter release];
[labelCoating release];
}
UILabel *labelBarcode = (UILabel *)[[cell contentView] viewWithTag:10];
UILabel *labelLength = (UILabel *)[[cell contentView] viewWithTag:11];
UILabel *labelThickness = (UILabel *)[[cell contentView] viewWithTag:12];
UILabel *labelDiameter = (UILabel *)[[cell contentView] viewWithTag:13];
UILabel *labelCoating = (UILabel *)[[cell contentView] viewWithTag:14];
UIImageView *thumbView = (UIImageView *)[[cell contentView] viewWithTag:15];
NSDictionary *pipe = [pipes objectAtIndex:row];
NSString *stringBarcode = [pipe objectForKey:@"Barcode"];
NSString *stringLength = [NSString stringWithFormat:@"Length: %@", [pipe objectForKey:@"Length"]];
NSString *stringThickness = [NSString stringWithFormat:@"Wall: %@",[pipe objectForKey:@"Thickness"]];
NSString *stringCoating = [NSString stringWithFormat:@"Coating: %@", [pipe objectForKey:@"Coating"]];
NSString *stringDiameter = [NSString stringWithFormat:@"Diameter: %@",[pipe objectForKey:@"Diameter"]];
[labelBarcode setText:stringBarcode];
[labelLength setText:stringLength];
[labelThickness setText:stringThickness];
[labelDiameter setText:stringDiameter];
[labelCoating setText:stringCoating];
[thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]];
return cell;
}特别是当我加载UITableViewController时,会发生延迟。请注意,我没有分配任何东西,我只在应用程序加载时分配数据(从plist),整个应用程序内存消耗为1.56mb。
发布于 2011-10-20 05:14:15
我也遇到过与图像视图进行某种大小调整相关的类似问题。我会先检查一下。如果您的thumbView正在调整大小,那么调整大小本身就会导致表格在滚动时出现滞后。如果是这种情况,您可以尝试预先渲染调整为正确大小的图像。我在这里假设,在只有两个单元格的情况下,你不会把任何一个单元格从表中拉出来。其他关于创建图像的时间的答案只有在你多次创建图像时才适用,这只会在细胞被重用时发生。
发布于 2011-10-20 05:12:05
下面这行代码:
[thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]]; 是阻塞的,并且可能需要大量的时间,具体取决于它的数据量。在后台线程上执行此操作(可能是通过NSOperation),然后在准备好图像时重新加载行。
发布于 2011-10-20 05:13:10
试着把
[thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]];
在重用条件内。
https://stackoverflow.com/questions/7828184
复制相似问题