zoukankan      html  css  js  c++  java
  • swift学习

    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
    
  • 相关阅读:
    开心菜鸟系列学习笔记------javascript(5)
    开心菜鸟系列学习笔记-------javascript(3)
    开心菜鸟学习系列-----javascript(2)
    开心菜鸟学习系列笔记-----Javascript(1)
    开心菜鸟系列----函数作用域(javascript入门篇)
    开心菜鸟系列----变量的解读(javascript入门篇)
    开心菜鸟笔记系列---正则表达式笔记(入门篇)
    开心菜鸟笔记系列---博客开篇贴!(求支持)
    在开启bin-log日志下Mysql报错
    开心菜鸟学习系列学习笔记------------nodejs util公共函数
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6492144.html
Copyright © 2011-2022 走看看