这是我用来生成UITableViewCell的代码。我正在iOS6 / iOS7中测试。如果我删除有关UIImageView的代码,UITableView滚动就好了。但是随着UIImageView它的滞后和痉挛。我正在寻找一些解决方案来解决滞后问题。提前谢谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BubbleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label;
UIImageView *bubbleLeft;
UIImageView *bubbleRight;
if(cell == nil)
{
cell = [[
UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
label = [[UILabel alloc] init];
label.tag = 1;
NSString *fileLeft = [[NSBundle mainBundle] pathForResource:@"chatBubbleGray.png" ofType:nil];
NSString *fileRight = [[NSBundle mainBundle] pathForResource:@"chatBubbleBlue.png" ofType:nil];
UIImage
*bubbleImageLeft = [[
UIImage imageWithContentsOfFile:fileLeft] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 22, 20, 22)];
UIImage
*bubbleImageRight = [[
UIImage imageWithContentsOfFile:fileRight] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 22, 20, 22)];
bubbleLeft = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
[bubbleLeft setImage:bubbleImageLeft];
bubbleLeft.tag=2;
bubbleRight = [[UIImageView alloc] initWithFrame:CGRectMake(320 - 20, 10, 10, 10)];
[bubbleRight setImage:bubbleImageRight];
bubbleRight.tag=3;
[cell addSubview:bubbleLeft];
[cell addSubview:bubbleRight];
[cell addSubview:label];
}else{
label = (UILabel *)[cell viewWithTag:1];
bubbleLeft = (UIImageView *)[cell viewWithTag:2];
bubbleRight = (UIImageView *)[cell viewWithTag:3];
}
// set frame to largest size you want
label.numberOfLines = 0;
label.
backgroundColor = [UIColor clearColor];
label.
text
= [[[
currentDictionary objectAtIndex:indexPath.row] objectForKey:@"string"] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [label.text sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];
if (indexPath.row%2==0) {
bubbleRight.hidden = YES;
bubbleLeft.hidden = NO;
bubbleLeft.frame = CGRectMake(10, 10, expectedLabelSize.width+20, expectedLabelSize.height+20);
label.frame = CGRectMake(
20, 20,
expectedLabelSize.width, expectedLabelSize.height);
}else{
bubbleLeft.hidden = YES;
bubbleRight.hidden = NO;
bubbleRight.frame = CGRectMake(320 - expectedLabelSize.width - 30, 10, expectedLabelSize.width+20, expectedLabelSize.height+20);
label.frame = CGRectMake(
320 - expectedLabelSize.width - 20, 20,
expectedLabelSize.width, expectedLabelSize.height);
}
// Configure the cell...
return cell;
}
编辑:
进一步检查代码显示滞后实际上是由这段代码引起的:
bubbleLeft.frame = CGRectMake(10, 10, expectedLabelSize.width+20, expectedLabelSize.height+20);
答案 0 :(得分:2)
似乎resizableImage导致了我之前所说的滞后。问题是我试图使用这段代码:
UIImage imageWithContentsOfFile:fileLeft] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 22, 20, 22)];
是
的图片 47x36
。
这意味着我的帽子负向翻转可拉伸部分导致滞后。作为进一步的信息,resizableImageWithCapInsets:
可以通过留出1px x 1px
可伸缩区域来实现最佳性能。
答案 1 :(得分:1)
您可以做的一件简单事情是使用UIImages
而不是imageNamed:
创建imageWithContentsOfFile:
。 imageNamed:
通常只会从第一个磁盘加载图像,然后对其进行缓存,而imageWithContentsOfFile:
将每隔时间转到磁盘。
如果这不能帮助您获得所需的性能,则可能必须将图像加载过程移至另一个执行线程。