Ios UIView&x27;是否未将添加到contentView?
我试图在代码中将一些视图添加到表视图单元格的contentView中。但是什么也没有出现。我的手机是空的。下面是我向自定义单元格的内容视图添加子视图的代码Ios UIView&x27;是否未将添加到contentView?,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在代码中将一些视图添加到表视图单元格的contentView中。但是什么也没有出现。我的手机是空的。下面是我向自定义单元格的内容视图添加子视图的代码 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupViews() } required init?(co
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)时,所有内容都会显示出来,包括我在代码中添加的视图。这里有一张图片告诉你我的意思:
但是,当我不添加任何虚拟视图时,就什么也看不到了……为什么以及如何解决这个问题?提前谢谢
编辑:
反馈单元和反馈框在类的顶部是这样实例化的
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()
视图层次结构调试器:
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
}
feedback\u title
和feedback\u box
在哪里实例化?编辑以回答您的问题!如何定义它们在contentView中的位置?好的,重新编辑一次,这样更有意义。您是否查看过XCode中的视图层次调试器以查看视图是否存在?