iOS 開發快速導引:TableView 和 CoreData【草】

來源:互聯網
上載者:User

標籤:

所有列表式的資料都是用 TableView 顯示的

預覽

待補充

原料
  • NSFetchedResultsController
    用來操作 NSFetchRequst,有執行查詢,監聽變化,資料緩衝等功能 
    • NSFetchRequest
      用來指定用什麼條件查哪個表,結果集怎麼排序 
      • NSSortDescriptor
        排序方式 必須 
      • NSPredicate
        謂語,其實就是查詢條件,可選 
  • UITableView 
    • UITableViewController
    • UITableVIew 必須指定 DataSource,只要沒特殊要求直接用這個 ViewController 就好了,這個類同時聲明了下面兩個協議
    • UITableViewDataSource
      顧名思義這個協議要實現資料來源相關特性 
    • UITableViewDelegate
      負責 TableView 的表現和動作的協議 
步驟
  1. Storyboard 裡拖一個 Table View Controller,這個 Table View Controller 內建一個 TableView 還有一個 TableViewCell
  2. 定義一個 UITableViewController 的子類,在 Storyboard 裡把剛才那個 Table View Controller 的 Class 設為這個子類
  3. 聲明一個 NSFetchedResultsController 私人變數,在 viewDidLoad 裡初始化。
  4. 重寫兩個方法
  5. numberOfRowsInSection 返回指定分組的行數,NSFetchedResultsController 知道答案
  6. cellForRowAtIndexPath 通過 NSFetchedResultsController 擷取到資料後設定儲存格的顯示值,然後再返回這個儲存格
  7. 可以煙霧測試 (Smoke Test)一下了
程式碼片段

只有 cellForRowAtIndexPath 的常規寫法有些特別,這裡只貼出它的例子

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 

      -> UITableViewCell {

        var cellId = "cellId" //屬性編輯器裡 給 TableViewCell 設定的 Identifier 

        var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell?

        if cell == nil{

            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId)

        }

        cell!.textLabel?.text="取出的值"

        return cell!

    }

連結
    • TableView 編程指南
    • Core Data 編程指南
    • UITableViewDataSource 協議參考
    • UITableViewDelegate 協議參考

iOS 開發快速導引:TableView 和 CoreData【草】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.