[英]UITableViewCell rotate lags/hangs if I set UIImage as background view
I am setting a UIImage, via a UIImageView as my background view of a subclassed UITableViewCell like so: 我通过UIImageView设置UIImage作为子类UITableViewCell的背景视图,如下所示:
-(void)awakeFromNib {
UIImage *backImg = [[UIImage imageNamed:@"CellBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(16, 132, 16, 16)];
UIImageView *imv = [[UIImageView alloc] initWithImage:backImg];
self.backgroundView = imv;
}
This works excellently, each cell is different in height (exposed via heightForRowAtIndexPath which calculates the height of a UILabel with text in it), and the background image resizes as I want the cell to. 效果非常好,每个单元格的高度都不同(通过heightForRowAtIndexPath暴露,该高度用于计算带有文本的UILabel的高度),并且背景图像的大小可以按照我希望的单元格进行调整。
However, when I rotate the device, the view hangs mid rotate, and takes 5-10 seconds to redraw in landscape, or crashes with no error. 但是,当我旋转设备时,视图会在旋转过程中挂起,并且需要5到10秒钟重新绘制风景,否则会崩溃而不会出现任何错误。 If I remove this imageview from the backgroundView, the rotate works excellently.
如果我从backgroundView中删除此imageview,则旋转效果非常好。 Both simulator and device.
模拟器和设备。
[Edit] Alternatively, I added the imageview as a subview of the cell.contentView - performance was better, but still laggy. [编辑]或者,我将imageview添加为cell.contentView的子视图-性能更好,但仍然很慢。
UIImage *backImg = [[UIImage imageNamed:@"CellBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(16, 132, 16, 16)];
UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imv.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imv.image = backImg;
[self.contentView addSubview:imv];
Additionally, as mentioned above, my UITableViewCell is a subclass. 另外,如上所述,我的UITableViewCell是一个子类。 The above code is in awakeFromNib, and I'm loading in my UITableViewCell like so:
上面的代码在awakeFromNib中,我像这样在UITableViewCell中进行加载:
// within initWithNibName: of UIViewcontroller:
cellLoader = [UINib nibWithNibName:@"MasterTableViewCell" bundle:[NSBundle mainBundle]];
// and UITableView method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MasterTableViewCell *cell = (MasterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MasterTableViewCell"];
if (cell == nil) {
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
return cell;
}
Am I doing something wrong? 难道我做错了什么? Any hints?
有什么提示吗?
Are you calling this every time you draw a cell? 每次绘制单元格时都打电话吗? This can really hurt your performance.
这确实会损害您的表现。 I would suggest only doing this when drawing a new cell.
我建议仅在绘制新单元格时这样做。 So it would look like
所以看起来像
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
UIImage *backImg = [[UIImage imageNamed:@"CellBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(16, 132, 16, 16)];
UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imv.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imv.image = backImg;
[self.contentView addSubview:imv];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.