UITableViewCell内部只放了一个UILabel,Cell的高度随着UILabel内容的高度变化而变化,可重写UITableView的委托方法动态调整高度,还要设置UILabel.numberOfLines = 0

1
2
3
4
5
6
7
8
9
10
11
12
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       if indexPath.row == 1 {
           //If it's content cell
           var boundSize = CGSizeMake(300, 500)
           var text = NSString(string: lblContent.text!)
           let attributes = NSDictionary(object: UIFont(name: "Arial", size: 15)!, forKey: NSFontAttributeName)
           let size = text.boundingRectWithSize(boundSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes as [NSObject : AnyObject], context: nil)
           return size.height + 10
       } else {
           return 44
       }
   }