Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Add demo using custom UITableViewCell
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Jan 9, 2017
1 parent acae25e commit d4477e9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
6 changes: 6 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
142E72801E2385BE001ACF3F /* CustomTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142E727E1E2385BE001ACF3F /* CustomTableViewCell.swift */; };
142E72811E2385BE001ACF3F /* CustomTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142E727E1E2385BE001ACF3F /* CustomTableViewCell.swift */; };
14308AA21CC66665000C21DF /* DATASource+NSFetchedResultsControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14308A9C1CC66665000C21DF /* DATASource+NSFetchedResultsControllerDelegate.swift */; };
14308AAA1CC66665000C21DF /* DATASource+UICollectionViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14308A9D1CC66665000C21DF /* DATASource+UICollectionViewDataSource.swift */; };
14308AB21CC66665000C21DF /* DATASource+UITableViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14308A9E1CC66665000C21DF /* DATASource+UITableViewDataSource.swift */; };
Expand Down Expand Up @@ -114,6 +116,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
142E727E1E2385BE001ACF3F /* CustomTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTableViewCell.swift; sourceTree = "<group>"; };
14308A9C1CC66665000C21DF /* DATASource+NSFetchedResultsControllerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DATASource+NSFetchedResultsControllerDelegate.swift"; sourceTree = "<group>"; };
14308A9D1CC66665000C21DF /* DATASource+UICollectionViewDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DATASource+UICollectionViewDataSource.swift"; sourceTree = "<group>"; };
14308A9E1CC66665000C21DF /* DATASource+UITableViewDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DATASource+UITableViewDataSource.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -295,6 +298,7 @@
1498B6061C3BC9E80066FAC8 /* DataModel.xcdatamodeld */,
1498B6051C3BC9E80066FAC8 /* CollectionCell.swift */,
44A8DB5D1DB77E62009EF5A7 /* Helper.swift */,
142E727E1E2385BE001ACF3F /* CustomTableViewCell.swift */,
);
path = Library;
sourceTree = "<group>";
Expand Down Expand Up @@ -763,6 +767,7 @@
14F2858E1DB9859A00028AD4 /* CollectionViewMultipleCellIdentifiers.swift in Sources */,
44B91D281DB774B6009DB4B7 /* DATASource+NSFetchedResultsControllerDelegate.swift in Sources */,
447D2EEC1DB78559006CBF1C /* CollectionViewControllerWithSections.swift in Sources */,
142E72801E2385BE001ACF3F /* CustomTableViewCell.swift in Sources */,
14663CA61DB97F2D00E8ABFC /* TableViewControllerWithSectionsWithoutIndex.swift in Sources */,
44B91D2E1DB774BF009DB4B7 /* DATASourceCollectionHeaderView.swift in Sources */,
447D2EE71DB784F9006CBF1C /* OptionsController.swift in Sources */,
Expand All @@ -788,6 +793,7 @@
44B91D2D1DB774BD009DB4B7 /* DATASource+UITableViewDataSource.swift in Sources */,
44B91D291DB774B6009DB4B7 /* DATASource+NSFetchedResultsControllerDelegate.swift in Sources */,
44B91D271DB774B4009DB4B7 /* DATASourceDelegate.swift in Sources */,
142E72811E2385BE001ACF3F /* CustomTableViewCell.swift in Sources */,
4471D4E51DB7822C00DE1EBE /* FooterExampleView.m in Sources */,
44B91D001DB7729D009DB4B7 /* main.m in Sources */,
44A8DB5F1DB77E62009EF5A7 /* Helper.swift in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions Library/CustomTableViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import UIKit

class CustomTableViewCell: UITableViewCell {
public static let Identifier = "CustomTableViewCell"

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
18 changes: 6 additions & 12 deletions SwiftDemo/TableViewController/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class TableViewController: UITableViewController {
NSSortDescriptor(key: "name", ascending: true),
]

let dataSource = DATASource(tableView: self.tableView, cellIdentifier: "Cell", fetchRequest: request, mainContext: self.dataStack.mainContext)
dataSource.delegate = self
let dataSource = DATASource(tableView: self.tableView, cellIdentifier: CustomTableViewCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext) { cell, item, indexPath in
let cell = cell as! CustomTableViewCell
cell.textLabel?.text = item.value(forKey: "name") as? String ?? ""
}

return dataSource
}()
Expand All @@ -30,22 +32,14 @@ class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: CustomTableViewCell.Identifier)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(TableViewController.saveAction))
self.tableView.dataSource = self.dataSource

let object = self.dataSource.objectAtIndexPath(IndexPath(row: 0, section: 0))!
print(object)
_ = self.dataSource.objectAtIndexPath(IndexPath(row: 0, section: 0))
}

func saveAction() {
Helper.addNewUser(dataStack: self.dataStack)
}
}

extension TableViewController: DATASourceDelegate {

func dataSource(_ dataSource: DATASource, configureTableViewCell cell: UITableViewCell, withItem item: NSManagedObject, atIndexPath indexPath: IndexPath) {
cell.textLabel?.text = item.value(forKey: "name") as? String ?? ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class TableViewControllerWithSections: UITableViewController {
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(TableViewController.saveAction))
self.tableView.dataSource = self.dataSource

let object = self.dataSource.objectAtIndexPath(IndexPath(row: 0, section: 0))!
print(object)
}

func saveAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class TableViewControllerWithSectionsWithoutIndex: UITableViewController {
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(TableViewController.saveAction))
self.tableView.dataSource = self.dataSource

let object = self.dataSource.objectAtIndexPath(IndexPath(row: 0, section: 0))!
print(object)
}

func saveAction() {
Expand Down

0 comments on commit d4477e9

Please sign in to comment.