swift學習 - tableView自適應高度2(SnapKit Layout)


SnapKit是Swift中自動布局的框架,相當於Objective-C中的Masonry

下面是tableView自定義cell,使用SnapKit布局的效果圖:

詳細代碼如下:

TYCustomCell.swift

import UIKit
import SnapKit

class TYCustomCell: UITableViewCell {
    var imgView: UIImageView?
    var titleLab:UILabel?
    var despLab:UILabel?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        //初始化頭像
        imgView = UIImageView()
        imgView?.image = UIImage.init(named: "img.jpg")
        imgView?.layer.borderColor = UIColor.gray.cgColor
        imgView?.layer.borderWidth = 1.0
        self.addSubview(imgView!)
        
        //頂部的label 初始化
        let label1 = UILabel()
        label1.font = .systemFont(ofSize: 15)
        label1.textColor = .red
        self.addSubview(label1)
        titleLab = label1
        
        //底部的label 初始化
        let label2 = UILabel()
        label2.font = .systemFont(ofSize: 14)
        label2.textColor = .black
        label2.numberOfLines = 0
        self.addSubview(label2)
        despLab = label2;
        
        //設置布局 SnapKit  --- >相當去Objective-C中的Masonry
        imgView?.snp.makeConstraints({ (make) in
            make.top.left.equalTo(10)
            make.width.height.equalTo(40)
        })
        
        label1.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo((imgView?.snp.right)!).offset(10)
            make.right.equalTo(-10)
            make.height.equalTo(21)
        }
        
        label2.snp.makeConstraints { (make) in
            make.top.equalTo(label1.snp.bottom).offset(10)
            make.left.equalTo((imgView?.snp.right)!).offset(10)
            make.right.equalTo(-10)
            make.bottom.equalTo(-10)
        }
        
    }

}

ViewController.swift

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var tableView:UITableView?;
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        setupTableView()
    }


    func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView?.delegate = self;
        tableView?.dataSource = self;
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView?.register(TYCustomCell.self, forCellReuseIdentifier: "TYCustomCell")
        view.addSubview(tableView!)
        tableView?.estimatedRowHeight = 44.0
        tableView?.rowHeight = UITableViewAutomaticDimension
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "TYCustomCell", for: indexPath) as! TYCustomCell
//        cell.textLabel?.text = "xxxx\(indexPath.row)"
        cell.titleLab?.text = "我是假數據"
        if indexPath.row%2==0 {
            cell.despLab?.text = "我是假數據我是假數據我是假數據"
        }
        else
        {
            if indexPath.row%3 == 0{
                cell.despLab?.text = "我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據"
            }
            else{
                cell.despLab?.text = "我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據我是假數據"
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("第\(indexPath.row)行被點擊了")
    }

}

重點:swift中tableView如果創建自定義cell,swift中如何使用SnapKit框架進行控件布局
自動計算高度代碼:

  tableView?.estimatedRowHeight = 44.0
  tableView?.rowHeight = UITableViewAutomaticDimension


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM