Proper way to determine UITableViewCell width for grouped custom cells?

拥有回忆 提交于 2019-12-05 15:39:14

You should be able to get the appropriate dimensions from the cell's contentView subview. But you should normally just set the sizes and autoresizing masks on your subviews so that the cell will appear correctly no matter what the specific width ends up being.

None of this seemed to work for me; I have a custom cell defined in a xib with a graphic against the left hand edge (20px in on iPhone), looked fine on iPhone. But on iPad the graphic was overlapping the left grouped section border! To fix it I set an outlet on the Horizontal Space constraint (leading) to the image, then tweaked that in the code for the UITableViewCell subclass:

- (void)setFrame:(CGRect)frame {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.leadingSpaceConstraint.constant = 40;
    }
    [super setFrame:frame];
}

And fiddled the number until the gap was just right. :)

I realise setFrame: may not have been the ideal place for this code, but it worked- so it stayed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!