Xcode 7 beta 5, Swift 2: UITableViewCell's sub

2020-07-22 20:05发布

问题:

Following phenomena happens when using Xcode 7 beta 5 and Swift 2:

When using a custom UICollectionViewCell that is created in the storyboard, the cell's subviews are not added to the cell's contentView. Thus the cell remains blank on runtime.

If I however create a custom cell class for the cell and then programmatically add the subviews to the contentView and set their frame the cell's content is displayed:

class Cell : UITableViewCell {

    @IBOutlet weak var label: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        NSLog("subiews.count=%d", contentView.subviews.count) // prints "0"

        contentView.subviews.count
        contentView.addSubview(label)

        label.frame = CGRect(x: 0, y: 0, width: 200, height: 21)
    }
}

Again, without manually adding the label (that has been added in the storyboard!) and setting its frame, it would not be visible at runtime! In the storyboard the label is a subview of the content view. At run time it is not.

I cannot observe this behavior in latest Xcode 6 with Swift 1.2.

Can somebody confirm this silly behavior? And maybe provide an easier workaround?

Edit: Luckily view constraints on the cell's subviews are applied after these views have been added programmatically to contentView. Thus at least manually setting their frames is not necessary.

回答1:

There is a similar question here UITableView Empty with iOS 9 beta 5 update

And my answer for it https://stackoverflow.com/a/32052154/2674336

I can't say if this is a universal solution, but in the exact same scenario (tablviewcell content empty at runtime after updating to XCode 7 beta 5) this solved it for me:

I had to go through every single item inside the content view (including all constraints) and tick the checkbox "Installed" in the properties inspector. Initially only wR hR was checked.