Created
April 9, 2016 01:26
-
-
Save joshavant/048a08d889ea15ca24b31b13d7f2d9e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
func namedModels() -> [ModelObject] { | |
let names: [String] = UIFont.familyNames() | |
return names.map(ModelObject.init) | |
} | |
class ModelObject: NSObject { | |
let name: String | |
init(name: String) { self.name = name } | |
func displayName() -> String { return self.name } | |
} | |
class ViewController: UIViewController, UITableViewDataSource { | |
static let CellIdentifier = "MyIdentifier" | |
@IBOutlet var tableView: UITableView! | |
var tableData: [[ModelObject]] = [] | |
let collation = UILocalizedIndexedCollation.currentCollation() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.tableView.dataSource = self | |
let objects = namedModels() | |
self.tableData = self.partitionCollatedObjects(objects, collationStringSelector: #selector(ModelObject.displayName)) | |
} | |
private func partitionCollatedObjects<T where T: AnyObject>(objects: [T], collationStringSelector: Selector) -> [[T]] { | |
var sections: [[T]] = Array(count: collation.sectionTitles.count, repeatedValue: [T]()) | |
let sortedObjects = collation.sortedArrayFromArray(objects, collationStringSelector: collationStringSelector) as! [T] | |
for object in sortedObjects { | |
let index = collation.sectionForObject(object, collationStringSelector: collationStringSelector) | |
sections[index].append(object) | |
} | |
return sections | |
} | |
// MARK: UITableViewDataSource | |
func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return self.collation.sectionTitles.count | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.tableData[section].count | |
} | |
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { | |
return collation.sectionIndexTitles | |
} | |
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
let showSection = self.tableData[section].count != 0 | |
return showSection ? self.collation.sectionTitles[section] : nil | |
} | |
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { | |
return self.collation.sectionForSectionIndexTitleAtIndex(index) | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier(ViewController.CellIdentifier)! | |
let model = self.tableData[indexPath.section][indexPath.row] | |
// configure cell | |
cell.textLabel?.text = model.displayName() | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment