美文网首页
27.swift-UITableView(系统)

27.swift-UITableView(系统)

作者: ChaosHeart | 来源:发表于2021-07-27 14:13 被阅读0次
    
    import UIKit
    
    class ViewController: UIViewController{
    
        //MARK:- 懒加载
        ///懒加载表视图
        lazy var tableV  = UITableView.init();
        
        //MARK: 系统回调
        override func viewDidLoad() {
            super.viewDidLoad()
        
            //
            setupTableView();
        }
    
    }
    
    ///extension 与 OC 中category 类似, 只能扩充方法
    
    
    //MARK:- 界面
    extension ViewController {
        ///设置tableView
        func setupTableView() {
            //添加tableView
            view.addSubview(tableV);
            //frame
            tableV.frame = view.bounds;
            //设置代理
            tableV.delegate = self;
            //设置数据源
            tableV.dataSource = self;
            //注册cell
            tableV.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "systemCell")
        }
    }
    
    //MARK:- 表视图的数据源和代理
    extension ViewController: UITableViewDelegate,UITableViewDataSource {
        
        ///区数
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1;
        }
        ///行数
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10;
        }
        ///展示cell
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            //创建cell
            let cell = tableView.dequeueReusableCell(withIdentifier: "systemCell", for: indexPath)
            //赋值
            cell.textLabel?.text = "测试数据:\(indexPath.row)";
            //返回
            return cell;
        }
        ///点击cell
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print("点击:",indexPath.row);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:27.swift-UITableView(系统)

          本文链接:https://www.haomeiwen.com/subject/mzrzlltx.html