简体   繁体   English

Xcode 7 beta 5,Swift 2:UITableViewCell的子视图在运行时未添加到contentView

[英]Xcode 7 beta 5, Swift 2: UITableViewCell's subviews are not added to the contentView at runtime

Following phenomena happens when using Xcode 7 beta 5 and Swift 2: 使用Xcode 7 beta 5和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 . 使用在故事板中创建的自定义UICollectionViewCell时,单元格的子视图不会添加到单元格的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: 但是,如果我为单元格创建自定义单元格类,然后以编程方式将子视图添加到contentView并设置其frame ,则会显示单元格的内容:

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. 在使用Swift 1.2的最新Xcode 6中,我无法观察到这种行为。

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 . 编辑:幸运的是,在将这些视图以编程方式添加到contentView之后,将应用对单元格子视图的视图约束。 Thus at least manually setting their frames is not necessary. 因此,至少手动设置它们的帧是不必要的。

There is a similar question here UITableView Empty with iOS 9 beta 5 update 这里有一个类似的问题UITableView Empty与iOS 9 beta 5更新

And my answer for it https://stackoverflow.com/a/32052154/2674336 我的回答是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: 我不能说这是否是一个通用的解决方案,但在完全相同的情况下(更新到XCode 7 beta 5后运行时的tablviewcell内容为空)这解决了我:

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. 最初只检查了wR hR。 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode 7 Beta 5,UITableViewCells子视图从ContentView中裁剪出来 - Xcode 7 Beta 5, UITableViewCells subviews are clipped out of ContentView 从UIView上的UITextfield读取值,该值已添加为Contentview UITableviewCell - Read values from UITextfield's on UIView which is added as Contentview UITableviewCell UITableViewCell的backgroundView重叠contentView - UITableViewCell's backgroundView overlapping contentView 滚动后是否删除了添加到UITableViewCell的contentView的SubView? - SubView added to the contentView of UITableViewCell is removed after Scrolling? iOS 8:UITableViewCell.contentView不会在其中调整子视图的大小 - iOS 8: UITableViewCell.contentView doesn't resize subviews in it 添加子视图后,UITableViewCell contentView框架大小是否会自动增加 - does a UITableViewCell contentView frame size increase automatically after adding subviews Xcode 7 Beta 5-故事板缺少所有子视图 - Xcode 7 Beta 5 - All subviews missing from Storyboard 包含在UITableViewCell的contentView中的UIScrollView的UIView - UIView containing UIScrollView inside UITableViewCell's contentView 如何为UITableViewCell的contentView重新创建UIView - How to recreate a UIView for a UITableViewCell's contentView Swift 3(Xcode 8 beta 6)localizedStringWithFormat - Swift 3 (Xcode 8 beta 6) localizedStringWithFormat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM