Why are self.frame and self.contentView.frame often different for UITableViewCell?

I sprinkled

NSAssert(abs(self.frame.size.height-self.contentView.frame.size.height)<=1,@"Should be the same"); 

in different places when creating a returned UITableViewCell.

The result is often different. Sometimes 1 pixel, sometimes 2.

I wonder what the problem is? Is there something else in cellForRowAtIndexPath?

They start the same thing. No editing, etc.

Look at this simple sniper.

 BGDetailTableViewCell * cell= (BGDetailTableViewCell*)[tableView dequeueReusableCellWithIdentifier:[BGDetailTableViewCell reuseIdentifier]]; if (cell==nil) { cell = [[BGDetailTableViewCell alloc]init]; } else { NSAssert(abs(cell.frame.size.height-cell.contentView.frame.size.height)<=1,@"Should be the same"); //Sometimes this fail } NSOrderedSet *Reviews = [self.businessDetailed mutableOrderedSetValueForKey:footer.relationshipKey]; Review * theReview = [Reviews objectAtIndex:row]; cell.theReview = theReview; NSAssert(abs(cell.frame.size.height-cell.contentView.frame.size.height)<=1,@"Should be the same");//This one never fail right before returning cell return cell; `NSAssert(abs(cell.frame.size.height-cell.contentView.frame.size.height)<=1,@"Should be the same")`; never fails right before returning the cell. 

However, this fails after I crossed out the cell, sometimes the last one.

This is the result.

 (lldb) po cell $0 = 0x0c0f0ae0 <BGDetailTableViewCell: 0xc0f0ae0; baseClass = UITableViewCell; frame = (0 424; 320 91); hidden = YES; autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0xc01e800>> (lldb) po cell.contentView $1 = 0x0c086080 <UITableViewCellContentView: 0xc086080; frame = (10 1; 300 89); gestureRecognizers = <NSArray: 0xc0c7ee0>; layer = <CALayer: 0xc0ebbf0>> 

By the way, tableView is in grouped mode. I think this has something to do with it.

+6
source share
1 answer

Two rectangles are in different coordinate systems and do not necessarily coincide.

cell.frame refers to the rect cell in its observation coordinate system ( cell.superview ). Cell Watch is a UITableView. The cell frame will be controlled by a table view in order to lay it out correctly. This also includes changing the height according to the rowHeight property or the value returned by the tableView:heightForRowAtIndexPath: delegate method.

contentView cell is "inside" the cell. This supervisor is the cell itself, which has its own local coordinate system for its subzones. They are not processed by the tableView, but by the restrictions set by the cell on it (for example, by your subclass of cells). Your subclass can implement layoutSubviews so that the size of the contentView what you want.

If you want your contentView to match the height (and borders) of your cell, in your UITableViewCell subclass, execute layoutSubviews like this:

 -(void)layoutSubviews { self.contentView.frame = self.bounds; } 

You can make any changes to the contentView frame that you want, but consider that you need to do this with respect to the local coordinate system using the bounds supervisor instead of frame .

+4
source

All Articles