tableview创建(swift)

    xiaoxiao2021-04-11  40

    //

    //  ViewController.swift

    //  tableview

    //

    //  Created by 洪福清 on 2017/1/17.

    //  Copyright © 2017 BJTYL. All rights reserved.

    //

    import UIKit

    class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

        //MARK:- 懒加载的属性

        ///tablew 的属性

        lazy var tableview : UITableView = UITableView()

        

        //MARK:- 系统回调函数

        override func viewDidLoad() {

            super.viewDidLoad()

            

            self.setupUI()

        }

        

    }

    extension ViewController {

        

        func setupUI() -> Void {

            //添加view

            view.addSubview(tableview)

            

            //设置frame

            tableview.frame = view.bounds

            

            //设置数据源

            tableview.dataSource = self

            

            tableview.delegate = self

        }

        

    }

    //extension ViewController : UITableViewDataSource,UITableViewDelegate {

    //给加个扩展为了好看些:类似occateory

    extension ViewController {

        //MARK:- tableview的数据源和代理方法

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return 20

        }

        

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let Cellid = "cellid"

            var cell = tableview.dequeueReusableCell(withIdentifier: Cellid)

            if cell == nil {

                cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: Cellid)

            }

            

            cell?.textLabel?.text = "测试数据\(indexPath.row)"

            return cell!

            

        }

        

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            print("点击了\(indexPath.row)")

        }

        

    }

    转载请注明原文地址: https://ju.6miu.com/read-666628.html

    最新回复(0)