I have a table view with a basic autolayout for cells.My issue is that cell is autoresizing based on subviews, but it doesn't autoresize based on UILabel text, cell just stays at minimum height regardless of label text.I am really desperate as I have tried EVERY approach i could find on stack overflow and was struggling with this for 3 days.
//!--------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;
}
}
in tableViewCellForRowAtIndexPath (its basically filling in the content, I dont think problem is here ,but just in case)
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