动态tableView单元格高度swift IOS8

时间:2015-05-23 18:05:38

标签: ios uitableview swift nslayoutconstraint

我正在尝试创建tableView,假设根据messageLabel自动调整高度。 IOS8使这变得更容易,但我似乎无法实现这一点。到目前为止,我已经使用xib文件创建了tableViewCell自定义类,如下所示:

enter image description here

然后设置messageLabel.numberOfLines = 0

viewController中的

我在viewDidLoad

中设置了以下内容
tableView.estimatedRowHeight = 80.0
tableView.rowHeight = UITableViewAutomaticDimension

最后是delegate methods

func tableView(tableView:UITableView, numberOfRowsInSection section:Int)->Int
{

    return 1

}



func numberOfSectionsInTableView(tableView:UITableView)->Int
{
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCellWithIdentifier("TwitterPlainCell", forIndexPath: indexPath) as! TwitterPlainCell
    let url = NSURL(string: "https://pbs.twimg.com/profile_images/507493048062713856/KaWKfdgW.jpeg")
    let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    // cell.backgroundColor = UIColor(rgba: "#f6f7f9")

    var name = "Olof Kajbjer"
    var userName = "@olofmCS"
    var topText = "\(name) \(userName)"
    var topCount = count(topText)
    var userCount = count(userName)
    var userNameLocation = topCount - userCount

    cell.profileImage.image = UIImage(data: data!)
    cell.dateLabel.text = "55m"

    var myMutableString = NSMutableAttributedString(string: topText, attributes: [NSFontAttributeName:UIFont(name: "PT Sans", size: 18.0)!])

    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor(rgba: "#A6B0B5"), range: NSRange(location:userNameLocation,length:count(userName)))
    myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "PT Sans", size: 13)!, range: NSRange(location:userNameLocation,length:count(userName)))

    // set label Attribute

    cell.topLabel.attributedText = myMutableString


    cell.messageLabel.text = "I FUCKING LOST AGAINST @olofmCS ON AIM AWP IN OVERTIME... GGgg"

    cell.setNeedsUpdateConstraints()
    cell.updateConstraintsIfNeeded()

    return cell
}

下面可以看到这个结果,为什么不调整单元高度?

enter image description here

1 个答案:

答案 0 :(得分:0)

问题与底部约束有关,我必须将其设置为>=或其他值