十分鐘學(xué)會(huì)cell自適應(yīng)高度(Swift)

cell自適應(yīng)高度

  • cell高度根據(jù)label高度變化而變化(代碼見(jiàn)下面)

重點(diǎn)講解

  • 自動(dòng)計(jì)算高度及設(shè)置預(yù)估行高
設(shè)置 tabelView 行高,自動(dòng)計(jì)算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        // 設(shè)置預(yù)估行高 --> 先讓 tableView 能滾動(dòng),在滾動(dòng)的時(shí)候再去計(jì)算顯示的 cell 的真正的行高,并且調(diào)整 tabelView 的滾動(dòng)范圍
        tableView.estimatedRowHeight = 300
  • 給cell的contentView布局,讓其底部與label底部對(duì)應(yīng)

使用前提

  • 用到了SnapKit框架
  • 如需要添加其他的控件,可利用自動(dòng)布局添加,注意讓cell的contentView的底部與最下面的控件的底部對(duì)應(yīng)即可

圖片展示運(yùn)行情況

![cell自適應(yīng)高度2.png](http://upload-images.jianshu.io/upload_images/1646838-ccf81342c88a58be.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

代碼

  • ViewController
import UIKit

class ViewController: UITableViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.tableView.delegate = self
        self.tableView.dataSource = self
        //注冊(cè)cell
        self.tableView.registerClass(WHTableViewCell.self, forCellReuseIdentifier: "cell")
        
        // 設(shè)置 tabelView 行高,自動(dòng)計(jì)算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        // 設(shè)置預(yù)估行高 --> 先讓 tableView 能滾動(dòng),在滾動(dòng)的時(shí)候再去計(jì)算顯示的 cell 的真正的行高,并且調(diào)整 tabelView 的滾動(dòng)范圍
        tableView.estimatedRowHeight = 300

    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
}

//數(shù)據(jù)源方法
extension ViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : WHTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as! WHTableViewCell
//cell.contentLable.text = "我有過(guò)多次這樣的奇遇,從天堂到地獄只在瞬息之間;每一朵可愛(ài)、溫柔的浪花,都成了突然崛起、隨即傾倒的高山。\n\n每一滴海水都變臉變色,剛剛還是那樣美麗、蔚藍(lán);旋渦糾纏著旋渦,我被拋向高空又投進(jìn)深淵……"
        cell.contentLable.text = "我有過(guò)多次這樣的奇遇,\n從天堂到地獄只在瞬息之間;\n每一朵可愛(ài)、溫柔的浪花,\n都成了突然崛起、隨即傾倒的高山。\n\n每一滴海水都變臉變色,\n剛剛還是那樣美麗、蔚藍(lán);\n旋渦糾纏著旋渦,\n我被拋向高空又投進(jìn)深淵……"
        return cell
    }
    
}
  • WHTableViewCell
import UIKit

class WHTableViewCell: UITableViewCell {

    //重寫cell init方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){
    
        self.contentView.addSubview(self.contentLable)
        
        contentLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self.contentView).offset(10)
            make.trailing.equalTo(self.contentView).offset(-10)
            make.top.equalTo(self.contentView).offset(10)
        }
        //重點(diǎn):給contentView布局,讓它的底部跟contentLable的底部一致
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.contentLable.snp_bottom).offset(10)
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)

        }
    }
    
    //懶加載label
    lazy var contentLable:UILabel = {()-> UILabel in
    
       let lable:UILabel = UILabel()
        lable.numberOfLines = 0
        lable.userInteractionEnabled = true

        return lable
    }()
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容