【精】表格(UITableView)總結(1):介紹與基礎實現

來源:互聯網
上載者:User

標籤:ios   swift   表格   uitableview   總結   

轉載請聲明出處:http://blog.csdn.net/jinnchang/article/details/45028267
1、表視圖(UITableView)UITableView 繼承自 UIScrollView。一個表視圖可以由多個分段(section)組成,每個分段可以有一個頭和尾。很多情況下表視圖只有一個分段,而且不顯示頭尾。表視圖本身也可以有一個頭(顯示在第一個分段之前)和一個尾(顯示在最後一個分段之後)。一個表視圖的整體元素結構如下:

(1)表視圖風格

UITableView 有以下兩種樣式(UITableViewStyle)

  • Plain(普通樣式)

  • Grouped(分組樣式)

上述兩種風格本質無區別,只是顯示樣式不同而已。

(2)表視圖分割線(Separator)

分割線有以下三種樣式(UITableViewCellSeparatorStyle)

  • None(無分割線)

  • SingleLine(單線條)

  • SingleLineEtched(帶浮雕效果的線條)

(3)表視圖單元格(Cell)

Cell 有以下四種顯示樣式(UITableViewCellStyle)

  • Default(左側顯示 textLabel,不顯示 detailTextLabel,最左邊可選顯示 imageView)

  • Value1(左側顯示 textLabel,右側顯示 detailTextLabel,最左邊可選顯示 imageView)

  • Value2(左側依次顯示 textLabel、detailTextLabel,最左邊可選顯示 imageView)

  • Subtitle(左側上方顯示 textLabel,左側下方顯示 detailTextLabel,最左邊可選顯示 imageView)

Cell 有以下四種選中樣式(UITableViewCellSelectionStyle)

  • None

  • Blue

  • Gray

  • Default

(4)附屬圖形(Accessory)

單元格的 Accessory 有以下五種樣式(UITableViewCellAccessoryType)

  • None(無附屬圖形)

  • DisclosureIndicator(小箭頭)

  • DetailDisclosureButton(詳細資料按鈕 + 指向右側的箭頭)

  • Checkmark(勾號)

  • DetailButton(詳細資料按鈕)

2、UITableView 協議iOS 遵循 MVC 設計模式,很多操作通過代理和外界溝通,UITableView 同理實現了以下兩種協議:
  • UITableViewDelegate
  • UITableViewDataSource
以下是官方分別對這兩個協議的原文介紹:
UITableViewDelegate has no required methods. It declares methods that allow the delegate to modify visible aspects of the table view, manage selections, support an accessory view, and support editing of individual rows in a table.
UITableViewDataSource has two required methods. The "numberOfRowsInSection" method tells the table view how many rows to display in each section, and the "cellForRowAtIndexPath" method provides the cell to display for each row in the table. Optional methods allow the data source to configure multiple sections, provide headers and/or footers, and support adding, removing, and reordering rows in the table.
3、表視圖控制器(UITableViewController)很多時候一個 UIViewController 中只有一個 UITableView,因此蘋果官方為了方便大家開發直接提供了一個 UITableViewController,這個控制器實現了 UITableView 資料來源和代理協議,內部定義了一個 tableView 屬性供外部存取,同時自動鋪滿整個螢幕、自動調整以方便我們的開發。如果需要用到 Table View 是不充滿全屏的話,我們應該使用 UIViewController 自己建立和維護 UITableView。
從一定層面上來說,除了方便、自動化佈建委託和資料來源屬性外,表視圖控制器沒有任何其它的優勢。
4、UITableView 基礎樣本
////  ViewController.swift//  UITableViewSample-Basic////  Created by jinnchang on 15/4/12.//  Copyright (c) 2015年 Jinn Chang. All rights reserved.//import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {        var tableView: UITableView!    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                tableView = UITableView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), style: .Plain)        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")        tableView.delegate = self        tableView.dataSource = self                self.view.addSubview(tableView)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return 20    }    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell        cell.textLabel?.text = "\(indexPath.row)"        return cell    }}
GitHub 上項目地址:https://github.com/jinnchang/SwiftSamples/tree/master/advanced/UITableViewSample-Basic
5、結語

參考資料如下:

About Table Views in iOS Apps

UITableView Class Reference

文章最後更新時間:2015年4月13日16:43:52

【精】表格(UITableView)總結(1):介紹與基礎實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.