使用Core Data Swift分割TableView和行 [英] Sectioning TableView and rows with Core Data Swift

查看:81
本文介绍了使用Core Data Swift分割TableView和行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sqlite中有两个表:

I have two tables in sqlite:

代码由XCode Generation生成:

The code was generated by XCode Generation:

class Event: NSManagedObject {

    @NSManaged var startDate: NSDate
    @NSManaged var details: EventDetail  //i think this property shoud be var details Array<EventDetail>  am i correct?
}

class EventDetail: NSManagedObject {

    @NSManaged var title: String
    @NSManaged var location: String
    @NSManaged var note: String
    @NSManaged var endDate: NSDate
    @NSManaged var event: NSManagedObject
}

我想将事件放在部分中,将事件详细信息放在中。

I want to put the events in section and the eventDetails in rows.

I创建了加载事件的方法:

I created the method to load events:

var eventList : Array<AnyObject> = []

func loadEvents(){
    let appDel : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let moc: NSManagedObjectContext = appDel.managedObjectContext!
    let eventMO = NSFetchRequest(entityName: "Event")
    eventMO.returnsObjectsAsFaults = false

    var err : NSErrorPointer = nil
    eventList = moc.executeFetchRequest(eventMO, error: err)!
    self.tblEvento.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    return eventList.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {       
    //return (eventList[section] as Event).details.count
   return //????? what can i put here
}

我不知道我能把什么放进去方法numbertOfRowsInSection用于定义节具有的行数。因为我无法访问details.count或类似的东西。

I don't know what can i put in the method numbertOfRowsInSection to define the number of rows the section have. Because i can't access the details.count or something liked this.

我认为还有其他方法可以做到这一点。我看到一些东西使用NSFetchedResultsController但没有成功。

I think there is other way to do this. I saw something as use NSFetchedResultsController but without sucess.

我会帮助你。

推荐答案

只是一个提示:如果你正在使用CoreData和UiTableView使用NSFetchedResultsController使事情变得更容易。如果你正在寻找一个起点&示例代码 - 只需在Xcode中创建一个新的master-detail-application项目,然后在对话框中打开Use Core Data。

Just a hint: If you're using CoreData and UiTableView use NSFetchedResultsController to make things much, much easier. If you're searching for a starting point & sample code - just create a new master-detail-application project in Xcode and turn on "Use Core Data" in the dialog.

现在,直接问你的问题:示例NSFetchResultsController的实现:

Now, straight to your question: an example "implementation" of the NSFetchResultsController:

var fetchedResultsController:NSFetchedResultsController {

var fetchedResultsController: NSFetchedResultsController {

  if _fetchedResultsController != nil {
       return _fetchedResultsController!
   }
   let fetchRequest = NSFetchRequest()
   // Edit the entity name as appropriate.
   let entity = NSEntityDescription.entityForName("Event", inManagedObjectContext: self.managedObjectContext!)
   fetchRequest.entity = entity

   // Set the batch size to a suitable number.
   fetchRequest.fetchBatchSize = 20

   // Edit the sort key as appropriate.
   let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
   let secondSortDescriptor = NSSortDescriptor(key: "title", ascending: true)

   let sortDescriptors = [sectionSortDescriptor, secondSortDescriptor]

   fetchRequest.sortDescriptors = sortDescriptors

   // Edit the section name key path and cache name if appropriate.
   // nil for section name key path means "no sections".
   let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "startDate", cacheName: nil)
   aFetchedResultsController.delegate = self
   _fetchedResultsController = aFetchedResultsController

   var error: NSError? = nil
   if !_fetchedResultsController!.performFetch(&error) {
       // Replace this implementation with code to handle the error appropriately.
       // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
       //println("Unresolved error \(error), \(error.userInfo)")
       abort()
   }

   return _fetchedResultsController!
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.fetchedResultsController.sections?.count ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionInfo = self.fetchedResultsController.sections![section] as NSFetchedResultsSectionInfo
    return sectionInfo.numberOfObjects
}

这篇关于使用Core Data Swift分割TableView和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆