我有一个带自定义单元格的UITableView。细胞内部有各种标签的“uiview”。
在第一次观看桌子时,细胞从未正确计算它们的高度。但是,如果我点击另一个视图并返回单元格高度,就像在预览图像中一样。
我的cellForRowAtIndexPath函数非常标准,如下所示:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell
if let frc = fetchedResultsController {
if let post = frc.objectAtIndexPath(indexPath) as? Post {
var name = post.user.username
cell.nameLabel.text = name
cell.subtitleLabel.text = "\(post.created_at.timeAgoFormat())"
let imageUrl = NSURL(string: post.user.profile_image_url)
cell.userImage!.hnk_setImageFromURL( imageUrl! )
cell.messageLabel.text = "Beta testing is never fun when it doesn't load like you wished it would.."
}
}
return cell
}
我尝试了各种其他stackoverflow答案但没有成功,例如:
cell.messageLabel.preferredMaxLayoutWidth = cell.messageLabel.frame.size.width
cell.cardView.sizeToFit()
cell.setNeedsLayout()
cell.layoutIfNeeded()
cell.messageLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.messageLabel.frame)
override func didMoveToSuperview() {
self.layoutIfNeeded()
}
messageLabel的行设置为0
viewDidLoad有
self.tableView.estimatedRowHeight = 300.0
self.tableView.rowHeight = UITableViewAutomaticDimension
自动布局约束均设置为具有单个顶部,底部,前导和尾随约束的视图,并且在模拟器中查看之前不会显示任何问题。这似乎有点奇怪,虽然我删除了除了和messageLabel和卡片UIView之外的所有视图,并且给了它们所有4个约束到superview仍然给出了错误。
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7ffe0445b7e0 V:[UIView:0x7ffe01e4a1e0]-(0)-| (Names: '|':UITableViewCellContentView:0x7ffe01e4a0c0 )>",
"<NSLayoutConstraint:0x7ffe044652a0 V:|-(10)-[UIView:0x7ffe01e4a1e0] (Names: '|':UITableViewCellContentView:0x7ffe01e4a0c0 )>",
"<NSLayoutConstraint:0x7ffe04472770 V:[UILabel:0x7ffe01e4a2f0'Beta testing is never fun...']-(8)-| (Names: '|':UIView:0x7ffe01e4a1e0 )>",
"<NSLayoutConstraint:0x7ffe04461fa0 V:[UIView:0x7ffe01d67d40]-(0)-[UILabel:0x7ffe01e4a2f0'Beta testing is never fun...']>",
"<NSLayoutConstraint:0x7ffe0446fa80 V:|-(0)-[UIView:0x7ffe01d67d40] (Names: '|':UIView:0x7ffe01e4a1e0 )>",
"<NSLayoutConstraint:0x7ffe04466cb0 V:[UIView:0x7ffe01d67d40(55)]>",
"<NSLayoutConstraint:0x7ffe044db690 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ffe01e4a0c0(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ffe04466cb0 V:[UIView:0x7ffe01d67d40(55)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-08-14 15:52:22.362 Birdie[14262:8821397] Unable to simultaneously satisfy constraints.
有关进一步参考,请参阅Post Cell
import UIKit
class PostCell: UITableViewCell {
@IBOutlet weak var cardView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!
@IBOutlet weak var userImage: UIImageView!
@IBOutlet weak var messageLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
self.cardSetup()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func cardSetup(){
self.cardView.alpha = 1
self.cardView.layer.masksToBounds = false
self.cardView.layer.cornerRadius = 2
self.cardView.layer.shadowOffset = CGSizeMake(CGFloat(-0.2), CGFloat(0.2))
self.cardView.layer.shadowRadius = 1
self.cardView.layer.shadowOpacity = 0.2
var path = UIBezierPath(rect: self.cardView.bounds)
self.cardView.layer.shadowPath = path.CGPath
self.backgroundColor = UIColor(red: 0.953, green: 0.953, blue: 0.953, alpha: 1)
}
}
还有其他想法吗? iOS 8模拟器,xcode 6.4