Reputation: 4575
I am setting a UIImage, via a UIImageView as my background view of a subclassed UITableViewCell like so:
-(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.
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. If I remove this imageview from the backgroundView, the rotate works excellently. Both simulator and device.
[Edit] Alternatively, I added the imageview as a subview of the cell.contentView - performance was better, but still laggy.
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. The above code is in awakeFromNib, and I'm loading in my UITableViewCell like so:
// 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?
Upvotes: 1
Views: 1071
Reputation: 1864
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];
}
Upvotes: 1