UITableViewCell的高度與UILabel自適應

UITableViewCell內部只放了一個UILabel,Cell的高度隨着UILabel內容的高度變化而變化,可重寫UITableView的委託方法動態調整高度,還要設置UILabel.numberOfLines = 0ide

 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
        }
    }
相關文章
相關標籤/搜索