Swift UITableViewCell中的UILabel不';t自动调整IOS 8的大小。(自动布局)

Swift UITableViewCell中的UILabel不';t自动调整IOS 8的大小。(自动布局),swift,uitableview,autolayout,sizetofit,Swift,Uitableview,Autolayout,Sizetofit,我有一个带有单元格基本自动布局的表视图。我的问题是单元格是基于子视图自动调整大小的,但它不会基于UILabel文本自动调整大小,无论标签文本如何,单元格都保持在最小高度。我真的很绝望,因为我已经尝试了所有可以在堆栈溢出上找到的方法,并为此挣扎了3天 //!--------Set youTableView------------------------// youTableView.delegate = self youTableView.dataSource = self youTableVie

我有一个带有单元格基本自动布局的表视图。我的问题是单元格是基于子视图自动调整大小的,但它不会基于UILabel文本自动调整大小,无论标签文本如何,单元格都保持在最小高度。我真的很绝望,因为我已经尝试了所有可以在堆栈溢出上找到的方法,并为此挣扎了3天

//!--------Set youTableView------------------------//
youTableView.delegate = self
youTableView.dataSource = self
youTableView.allowsSelection = false
youTableView.bounces = true
youTableView.estimatedRowHeight = 100
youTableView.rowHeight = UITableViewAutomaticDimension
youTableView.registerClass(CommentNFTableViewCell.self, forCellReuseIdentifier: "Comment Cell")
youTableView.registerClass(LikeNFTableViewCell.self, forCellReuseIdentifier: "Like Cell")
youTableView.registerClass(ResponseNFTableViewCell.self, forCellReuseIdentifier: "Response Cell")
youTableView.registerClass(FollowNFTableViewCell.self, forCellReuseIdentifier: "Follow Cell")
youTableView.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 5) // move separator line
youTableView.tableFooterView = UIView()
self.youTableView.addSubview(youRefreshControl)
youRefreshControl.addTarget(self, action: "Get_You_Data", forControlEvents: UIControlEvents.ValueChanged)
CommentNFTableViewCell.swift

class CommentNFTableViewCell: UITableViewCell,TTTAttributedLabelDelegate {
    var ScreenWidth = UIScreen.mainScreen().bounds.width
    var didSetupConstraints = false

    var row:Int = Int()

    var profilePicture:UIImageView = UIImageView()
    var commentLabel:TTTAttributedLabel = TTTAttributedLabel()

    var eventPicture:UIImageView = UIImageView()
    var dateLabel:UILabel = UILabel()
    var username:NSString!

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

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(profilePicture)
        self.contentView.addSubview(commentLabel)
        self.contentView.addSubview(dateLabel)
        self.contentView.addSubview(eventPicture)

        profilePicture.setTranslatesAutoresizingMaskIntoConstraints(false)
        commentLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        dateLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        eventPicture.setTranslatesAutoresizingMaskIntoConstraints(false)

        commentLabel.numberOfLines = 0
        commentLabel.font = UIFont(name: "Helvetica", size: 14)
        commentLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping


        dateLabel.numberOfLines = 2
        dateLabel.backgroundColor = ColorFromCode.standardBlueColor()
        dateLabel.textColor = UIColor.whiteColor()
        dateLabel.font = UIFont(name: "Helvetica", size: 14)
        dateLabel.layer.masksToBounds = true
        dateLabel.layer.cornerRadius = 3
        dateLabel.textAlignment = NSTextAlignment.Center

        eventPicture.layer.masksToBounds = true //without it its not gonna work
        eventPicture.layer.cornerRadius = 3

        profilePicture.layer.masksToBounds = true //without it its not gonna work
        profilePicture.layer.cornerRadius = 20


        var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: UIColor.blackColor()]
        self.commentLabel.activeLinkAttributes = attrs
        make_Layout()

    }

    func make_Layout(){
        var views = [
            "profilePicture": profilePicture,
            "commentLabel": commentLabel,
            "dateLabel": dateLabel,
            "eventPicture": eventPicture
        ]
        var metrics = [
        ]
        var H_Constraint0 = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[profilePicture(40)]-10-[commentLabel]-10-[eventPicture(40)]-10-|", options: nil, metrics: nil, views: views)
        var H_Constraint1 = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[profilePicture(40)]-10-[commentLabel]-10-[dateLabel(40)]-10-|", options: nil, metrics: nil, views: views)

        var V_Constraint0 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[profilePicture(40)]-|", options: nil, metrics: nil, views: views)
        var V_Constraint1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[commentLabel(>=10)]-|", options: nil, metrics: nil, views: views)
        var V_Constraint2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[dateLabel(40)]-|", options: nil, metrics: nil, views: views)
        var V_Constraint3 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[eventPicture(40)]-|", options: nil, metrics: nil, views: views)




        // height == width for picture


        self.contentView.addConstraints(H_Constraint0)
        self.contentView.addConstraints(H_Constraint1)
        self.contentView.addConstraints(V_Constraint0)
        self.contentView.addConstraints(V_Constraint1)
        self.contentView.addConstraints(V_Constraint2)
        self.contentView.addConstraints(V_Constraint3)
        self.contentView.setNeedsLayout()
        self.contentView.layoutIfNeeded()
        self.commentLabel.preferredMaxLayoutWidth = self.commentLabel.frame.size.width;
    }
}
在tableViewCellForRowAtIndexPath中(它基本上是填充内容,我认为问题不在这里,只是以防万一)


使用表单nib或故事板设置感谢您的建议,但我想要纯编程方法使用表单nib或故事板设置感谢您的建议,但我想要纯编程方法
                let Cell:CommentNFTableViewCell = self.youTableView.dequeueReusableCellWithIdentifier("Comment Cell", forIndexPath: indexPath) as! CommentNFTableViewCell

                Cell.row = indexPath.row
                // Labels

                var attributedString:NSMutableAttributedString = NSMutableAttributedString()

                var fromUserString:NSMutableAttributedString = NSMutableAttributedString()
                var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: ColorFromCode.standardBlueColor()]
                fromUserString = NSMutableAttributedString(string: youData[indexPath.row].fromUser!.username!, attributes:attrs)
                attributedString.appendAttributedString(fromUserString)

                var url:NSURL = NSURL(scheme: "pushuser", host: fromUserString.string, path: "/")!
                Cell.commentLabel.addLinkToURL(url, withRange: NSRange(location: 0,length: fromUserString.length))
                Cell.commentLabel.delegate = self
                Cell.commentLabel.tag = indexPath.row

                var content:NSMutableAttributedString = NSMutableAttributedString()
                attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: UIColor.blackColor()]
                content = NSMutableAttributedString(string: " has commented on your event: " + (youData[indexPath.row].content as String), attributes:attrs)
                attributedString.appendAttributedString(content)

                // Set Event Picture
                dispatch_async(dispatch_get_main_queue(), {
                    () -> Void in
                    Cell.DownloadEventPicture(&self.youData[indexPath.row].event!, row: indexPath.row)
                })
                Cell.UpdateEventPicture(self.youData[indexPath.row].event!, row: indexPath.row)
                Cell.UpdateProfilePicture(self.youData[indexPath.row])

                // Event Date

                let dateFormatter = NSDateFormatter()
                dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
                dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
                dateFormatter.doesRelativeDateFormatting = true
                Cell.dateLabel.text = dateFormatter.stringFromDate(self.youData[indexPath.row].event!.date)
                let date:NSDate = self.youData[indexPath.row].metadata!.creationTime()

                var dateString:NSMutableAttributedString = NSMutableAttributedString()
                attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: UIColor.lightGrayColor()]
                dateString = NSMutableAttributedString(string: DateToStringConverter.getRelativeDateString(date), attributes:attrs)
                attributedString.appendAttributedString(dateString)



                Cell.highlightMentionsInString(attributedString, withColor: ColorFromCode.standardBlueColor())
                Cell.highlightHashtagsInString(attributedString, withColor: ColorFromCode.standardBlueColor())

return Cell