Swift TableView的使用

一、基础的使用

创建一个列表,包含列表的头部和尾部,SectionHeader和SectionFooter。

import UIKit

class BaseTableViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    
    
   
    var tableView: UITableView!

    var datas = ["1","2","3","4"]
    
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        
        tableView = UITableView(frame: self.view.bounds, style: UITableView.Style.grouped)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableHeaderView = tableViewHeader()
        tableView.tableFooterView = tableViewFooter()
        self.view.addSubview(tableView)
      
    }
    
    func tableViewHeader() -> UIView{
    
    
        let label:UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 80))
        label.text = "我是TableView的头部"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 16)
        label.backgroundColor = UIColor.red
        label.textAlignment = NSTextAlignment.center
        return label
    }
    
    func tableViewFooter() -> UIView{
    
    
        let label:UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 80))
        label.text = "我是TableView的尾部"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 16)
        label.backgroundColor = UIColor.red
        label.textAlignment = NSTextAlignment.center
        return label
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    
    
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        let identifier = "CELL"
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: identifier)
        
        cell.textLabel?.text = datas[indexPath.row]
        cell.detailTextLabel?.text = "Test"
        
        return cell
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    
        NSLog("我被点击了%ld",indexPath.row)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
    
        return 30
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    
        let label:UILabel = UILabel.init(frame: CGRect(x: 10, y: 10, width: 100, height: 20))
        label.text = "我是Section的头部"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 16)
        label.backgroundColor = UIColor.orange
        label.textAlignment = NSTextAlignment.left
        return label
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    
    
        return 30
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    
    
        let label:UILabel = UILabel.init(frame: CGRect(x: 10, y: 10, width: 100, height: 20))
        label.text = "我是Section的尾部"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 16)
        label.backgroundColor = UIColor.orange
        label.textAlignment = NSTextAlignment.left
        return label
    }

}

猜你喜欢

转载自blog.csdn.net/u010545480/article/details/128041769