码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 开发快速导引:TableView 和 CoreData【草】

时间:2015-05-31 22:55:55      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

所有列表式的数据都是用 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. 可以冒烟测试一下了

代码片段

只有 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!

    }

链接

iOS 开发快速导引:TableView 和 CoreData【草】

标签:

原文地址:http://www.cnblogs.com/bemxself/p/4542700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!