简体   繁体   中英

UITableViewCell - setting background view and accessory in the initializer

I've browsed around a bit but didn't find any solution to my problem, so here I go: I have a custom UITableViewCell subclass - MyCell. I am overriding the initWithReuseIdentifier to set stuff like label fonts, backgrounds, etc. However, when I try to set self.accessoryType or self.backgroundView = myImageView in the init, it doesn't seem to have any effect.

I have moved the bit of code where I am creating the image view and setting the backgrounView in the layoutSubviews method in my cell class. This however causes the problem that the background view appears as the topmost view in the cell, ie covering the labels and everything else. Any help would be greatly appreciated.

You many need to start moving your subviews around. See -sendSubviewToBack: for more detail.

Note: this will make things fragile and you may need to be very careful about your logic to avoid iOS version specific problems.

You may also want to read A Closer Look at Table View Cells for more information about the subviews of a UITableViewCell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM