Super awesome Core Data driven UI for iOS written in Swift
AECoreDataUI was previously part of AERecord, so you may want to check that also.
When it comes to connecting your data with the UI, the best approach is to use
NSFetchedResultsController
.CoreDataTableViewController
wrapper from Stanford's CS193p is so great at it, that I've written it in Swift and madeCoreDataCollectionViewController
too in the same fashion.
- Core Data driven UITableViewController (UI automatically reflects data in Core Data model)
- Core Data driven UICollectionViewController (UI automatically reflects data in Core Data model)
You can also check demo project in AERecord.
CoreDataTableViewController
mostly just copies the code from NSFetchedResultsController
documentation page into a subclass of UITableViewController
.
Just subclass it and set it's fetchedResultsController
property.
After that you'll only have to implement tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
and fetchedResultsController
will take care of other required data source methods.
It will also update UITableView
whenever the underlying data changes (insert, delete, update, move).
import UIKit
import CoreData
class MyTableViewController: CoreDataTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
refreshData()
}
func refreshData() {
let sortDescriptors = [NSSortDescriptor(key: "timeStamp", ascending: true)]
let request = Event.createFetchRequest(sortDescriptors: sortDescriptors)
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: AERecord.defaultContext, sectionNameKeyPath: nil, cacheName: nil)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
if let frc = fetchedResultsController {
if let object = frc.objectAtIndexPath(indexPath) as? Event {
cell.textLabel.text = object.timeStamp.description
}
}
return cell
}
}
Same as with the tableView.
- Xcode 7.3+
- iOS 8.0+
-
Using CocoaPods:
pod 'AECoreDataUI'
-
github "tadija/AECoreDataUI"
-
Manually:
Just drag AECoreDataUI.swift into your project and start using it.
AECoreDataUI is released under the MIT license. See LICENSE for details.