美文网首页
swift tableView的基本使用

swift tableView的基本使用

作者: 越天高 | 来源:发表于2019-12-23 14:53 被阅读0次

import UIKit

class ViewController: UIViewController {
    lazy var tableView:UITableView = UITableView();
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.setupUI()
    }


}


//MARK: - 更页面设置相关 -

extension ViewController
{
    func setupUI()  {
        // 0.将tableView添加到控制器的View中
        self.view.addSubview(tableView)
        // 1.设置tableView的frame

        tableView.frame = self.view.bounds;
        // 2.设置数据源
        tableView.dataSource = self;
        // 3.设置代理

        tableView.delegate = self;
        tableView.backgroundColor = UIColor.orange
        
    }
}

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

extension ViewController : UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //创建cell
        let cellID = "CellID"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: cellID)
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellID)
        }
        cell?.textLabel?.text = "数据源:\(indexPath.row)"
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("ss选择了\(indexPath.row)行")
    }
}

相关文章

网友评论

      本文标题:swift tableView的基本使用

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