補充筆記04-tableview的簡單設置

tableview的簡單設置

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var Name: [String]?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Name = ["切爾斯", "里斯", "威爾斯"]
        
        //列表鋪滿整個view
        let tableView = UITableView(frame: self.view.bounds, style: .Plain)
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(tableView)
        
        //設置tableview頭的顏色
        let headView = UIView(frame: CGRect(x: 100, y: 0, width: 100, height: 100))
        headView.backgroundColor = UIColor.blueColor()
        tableView.tableHeaderView = headView
        
        //設置tableview尾的顏色
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 200))
        footerView.backgroundColor = UIColor.redColor()
        tableView.tableFooterView = footerView
    }
    
    //設置section的顯示個數
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Name!.count
    }
    
    //設置tableview的顯示個數
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
    
    //設置選擇點擊section顯示section的內容
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.section, indexPath.row)
        print(Name![indexPath.row])
    }
    
    //設置section的頭的顏色
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = UIColor.brownColor()
        return v
    }
    
    //設置section的頭的高度
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    
    //設置section的尾的高度
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 45
    }
    
    //設置section的尾的顏色
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v1 = UIView()
        v1.backgroundColor = UIColor.blackColor()
        return v1
    }
    
    //設置tableview列表顯示的內容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
            if cell == nil {
                cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
            }
        cell?.textLabel?.text = Name![indexPath.row]
        return cell!
    }

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容