UIView没有被添加到contentView?

时间:2016-03-25 17:34:25

标签: ios swift uitableview

我试图将代码中的一些视图添加到表格视图单元格的contentView中。然而,没有任何表现。我得到一个空单元格。下面是我将子视图添加到自定义单元格的内容视图的代码。

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    setupViews()

}

func setupViews() {
    //feedback_title setup
    feedback_title.text = Constants.IndividualStudiesPage.FEEDBACK_TITLE
    feedback_title.numberOfLines = 0
    feedback_title.lineBreakMode = .ByTruncatingTail

    feedback_title.preferredMaxLayoutWidth = standard_height_width

    //feedback_box setup
    feedback_box.text = Constants.IndividualStudiesPage.DEFAULT_FEEDBACK

    // TODO: Change to infinte (0) number of lines. Height constraint interferes with this
    feedback_box.numberOfLines = 2
    feedback_box.preferredMaxLayoutWidth = standard_height_width

    contentView.addSubview(feedback_title)
    contentView.addSubview(feedback_box)
}

这是有趣的事情。当我向故事板中的内容视图添加一些虚拟视图(如空UILabel)时,所有内容都会显示,包括我在代码中添加的视图。这是一张图片,向您展示我的意思: enter image description here

但是当我没有添加任何虚拟视图时,没有任何显示...为什么以及如何解决这个问题?提前致谢。

编辑:

feedback_cell和feedback_box在类的顶部实例化

class FeedbackCell: UITableViewCell {

var feedback_title : UILabel = UILabel.newAutoLayoutView()
var feedback_box : UILabel = UILabel.newAutoLayoutView()

newAutoLayoutView是一个PureLayout函数,它基本上只是将标签实例化为空视图。然后我在updateConstraints函数中使用PureLayout更新AutoLayout约束:

 override func updateConstraints() {
    if !did_update_constraints {
        //prevent labels from being compressed below intrinsic height but also from taking too much height
        NSLayoutConstraint.autoSetPriority(UILayoutPriorityRequired){
            self.feedback_title.autoSetContentHuggingPriorityForAxis(.Vertical)
            self.feedback_title.autoSetContentCompressionResistancePriorityForAxis(.Vertical)

            self.feedback_box.autoSetContentHuggingPriorityForAxis(.Vertical)
            self.feedback_box.autoSetContentCompressionResistancePriorityForAxis(.Vertical)
        }

        //height constraints
        feedback_title.autoSetDimension(.Height, toSize: standard_height_width)


        //feedback_title constraints
        feedback_title.autoPinEdgeToSuperviewMargin(.Leading)
        feedback_title.autoPinEdgeToSuperviewMargin(.Trailing, relation: NSLayoutRelation.LessThanOrEqual)
        feedback_title.autoPinEdgeToSuperviewMargin(.Top)

        //feedback_box constraints
        feedback_box.autoPinEdge(.Top, toEdge: .Bottom, ofView: feedback_title, withOffset: 10, relation: .GreaterThanOrEqual)
        feedback_box.autoPinEdgeToSuperviewMargin(.Leading)
        feedback_box.autoPinEdgeToSuperviewMargin(.Trailing, relation: NSLayoutRelation.LessThanOrEqual)
        feedback_box.autoPinEdgeToSuperviewMargin(.Bottom)

        did_update_constraints = true

    }

    super.updateConstraints()

查看层次结构调试器: enter image description here

Tableview代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    switch (indexPath.row) {
    case 0:
        return setupDescriptionCell(tableView, indexPath: indexPath)
    case 1:
        return setupFeedbackCell(tableView, indexPath: indexPath)
    case 2:
        return setupStatsCell(tableView, indexPath: indexPath)
    case 3:
        return setupSurveyCell(tableView, indexPath: indexPath)
    default:
        return setupDefaultCell(tableView, indexPath: indexPath)
    }
}


func setupFeedbackCell(tableView : UITableView, indexPath : NSIndexPath)->UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cell_IDs[1], forIndexPath: indexPath) as! FeedbackCell

    //in the future download feedback in view did load and adjust here

    return cell
}

0 个答案:

没有答案