8.2 UiTabelView

8.2 UiTabelView

整個Tabelview有頭部,和尾部。里面可能有數(shù)個分組section,分組中又有分組頭部和分組尾部,分組中又有很多行Cell

1.行高,默認44個像素
2.行數(shù)
3.每一行的內(nèi)容(數(shù)據(jù))

tableView,Cell ,Row,Section,主線程,加載數(shù)據(jù)

  1. 關鍵詞:tableView header footer section Cell /Row枚舉類型
  2. Cell、Header、Footer寬度一定與TableView相同x/y/width無效(TableView中)3. 同一個Cell對象會重復使用,在隊列中獲取空閑的Cell
    var cell = tableView.dequeueReusableCellWithIdentifier("cell")
  3. 幾個關鍵函數(shù):numberOfSectionsInTableViewnumberOfRowsInSectioncellForRowAtIndexPathdidSelectRowAtIndexPathviewForHeaderInSectionheightForHeaderInSection
  4. 建立一個類比建立數(shù)組或者字典要優(yōu)越的多,可以在敲代碼的時候彈出類中的成員變量比如: var channel_id: String!var channel_name: String!
  5. 在refreshTableView放入主線程中執(zhí)行方法一:self.tableView.performSelectorOnMainThread(#selector(self.tableView.self.reloadData), withObject: nil, waitUntilDone: false)方法二:self.performSelectorOnMainThread(#selector(self.refreshTableView), withObject: nil, waitUntilDone: true)
  6. tableView.reloadData() //重新加載
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var names: [String]?                  //定義一個數(shù)組

    override func viewDidLoad() {

        super.viewDidLoad()

        names = ["張三", "李四", "王五", "趙六"]

        

        //.Plain樣式默認沒有分隔,  .Grouped有分隔

        let tableView = UITableView(frame: self.view.bounds, style: .Grouped)

        tableView.dataSource = self

        tableView.delegate = self

        self.view.addSubview(tableView)

        

        //Cell、Header、Footer寬度一定與TableView相同

        //x/y/width無效,只有高度height有效

        let headView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))

        headView.backgroundColor = UIColor.redColor()

        tableView.tableHeaderView = headView         //定義頭部為紅色,50像素高

        

        let footView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))

        footView.backgroundColor = UIColor.greenColor()

        tableView.tableFooterView = footView         //定義尾部為綠色,50像素高

    }

    

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 2

    }

    //詢問某個section中有多少條數(shù)據(jù)

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

        return names!.count

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {

        

        //同一個Cell對象會重復使用

        //1. 在隊列中獲取空閑的Cell

        var cell = tableView.dequeueReusableCellWithIdentifier("cell")

        if cell == nil {

            //2. 創(chuàng)建可以重用的Cell對象

            cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")

        }

        

        //3. 設置內(nèi)容

        cell?.textLabel?.text = names![indexPath.row]

//        cell?.detailTextLabel?.text = "xxxxx"

        return cell!

    }

    

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {

        print(indexPath.section, indexPath.row)

        print(names![indexPath.row])

    }

//    

//    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

//        return "Section \(section) Header"       //顯示Section頭部名稱

//    }

//    

//    func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {

//        return "Section \(section) Footer"       //顯示Section尾部名稱

//    }

    

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) ->UIView? {

        let v = UIView()

        v.backgroundColor = UIColor.blueColor()

        return v                        //定義Section的頭部為藍色

    }

    

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) ->CGFloat {

        return 44.0                     //定義Section的頭部為44個像素高

    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容