Swift UITableView

UITableView

平常我們用的最多的列表展示應(yīng)該就是UITableView,UITableView繼承自UIScrollView,因此支持垂直滾動,而且性能極佳,加上本身的一些方法足夠讓我們運(yùn)用到許多地方。

1、基本用法
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
 
    var testArray = ["one","two","three","four"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self .makeTestTableView()
        
    }
    private lazy var tableView: UITableView = {
        let testTableView = UITableView()
        testTableView.delegate = self
        testTableView.dataSource = self
        // 木有后半部分分割線的顯示
        testTableView.tableFooterView = UIView()
        // 注冊
        testTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "testCellIden")
        return testTableView
    }()

    
    func makeTestTableView(){
        /* 備注下,之前是2.1之前是建議不用self的,但是最近2.1又提出加self這個(gè)提案
          https://github.com/apple/swift-evolution/blob/master/proposals/0009-require-self-for-accessing-instance-members.md
          目前還是看個(gè)人習(xí)慣吧
        */
        self.tableView.frame = self.view.frame
        self.view .addSubview(self.tableView)

    }
    
   //MARK: UITableView--Delegate
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.testArray.count
    }
  
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCellWithIdentifier("testCellIden")
        cell?.textLabel?.text = "there is \(self.testArray[indexPath.row])"
        return cell!
    }

}
2、自定義的情況

注意registerClassdequeueReusableCellWithIdentifier兩個(gè)方法的不同就OK了

testTableView.registerClass(TestTableViewCell.self, forCellReuseIdentifier: "testCellIden")
let cell = tableView .dequeueReusableCellWithIdentifier("testCellIden") as! TestTableViewCell

假如是XIB

//nibName指的是我們創(chuàng)建的Cell文件名
let nib = UINib(nibName: "testListCell", bundle: nil)   
self.tableView.registerNib(nib, forCellReuseIdentifier: "iden")
3、要注意的地方

3-1、其他可能用到的代理方法

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    // 選中的情況
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // 刪除 或者  說編輯
    let index=indexPath.row
    self.testArray.removeAtIndex(index)
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
}

3-2、當(dāng)Cell 高度不定的時(shí)候

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
     //    public func boundingRectWithSize(size: CGSize, options: NSStringDrawingOptions, attributes: [String : AnyObject]?, context: NSStringDrawingContext?) -> CGRect
          return
}

UITableView需要用的太多了,繼續(xù)學(xué)習(xí)中···

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

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

  • AppDelegate:func application(_ application: UIApplication...
    任任任任師艷閱讀 1,734評論 0 0
  • var tableview = UITableView() // 創(chuàng)建可變數(shù)組 var datas: NSMut...
    小_蠟筆閱讀 3,425評論 0 0
  • 不知從何時(shí)起,內(nèi)心少了一份由內(nèi)而外的淡然平和。抑或這種體驗(yàn)從未全然享有,只是偶爾靈光乍現(xiàn)。大多數(shù)時(shí)候,內(nèi)心被一種莫...
    一個(gè)人跳舞丫閱讀 221評論 1 1
  • 1.確定方向不走冤枉路 俗話說:"馬壯車好不如方向?qū)?quot;,這句話的典故來自春秋戰(zhàn)國時(shí)期,有位夫子備了很多物品,欲前往...
    張敏捷_閱讀 392評論 0 1
  • 淚的輪回 眼淚 以永恒的溫度 凈化著人們的心境 而眼淚 以痛苦的輪回 升華著人們的靈魂
    一了0820閱讀 263評論 0 3

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