From bc0e18f7f1fee0769a6c3aeb5031e3af9b398102 Mon Sep 17 00:00:00 2001 From: Sarunas Kazlauskas Date: Fri, 8 Jan 2016 16:07:27 +0200 Subject: [PATCH] Add sections support --- ExampleApp/AppDelegate.swift | 13 ++++++++---- Framework/CellConfigurator.swift | 8 +++++++ .../ConfigurableTableViewController.swift | 21 ++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/ExampleApp/AppDelegate.swift b/ExampleApp/AppDelegate.swift index 03bf3ee..1265ea2 100644 --- a/ExampleApp/AppDelegate.swift +++ b/ExampleApp/AppDelegate.swift @@ -15,12 +15,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - let viewController = ConfigurableTableViewController(items: [ + let firstSectionConfigurator = SectionConfigurator(cellConfigurators: [ CellConfigurator(viewData: TextCellViewData(title: "Foo")), - CellConfigurator(viewData: ImageCellViewData(image: UIImage(named: "Apple")!)), + CellConfigurator(viewData: ImageCellViewData(image: UIImage(named: "Apple")!)) + ]) + + let secondSectionConfigurator = SectionConfigurator(cellConfigurators: [ CellConfigurator(viewData: ImageCellViewData(image: UIImage(named: "Google")!)), - CellConfigurator(viewData: TextCellViewData(title: "Bar")), - ]) + CellConfigurator(viewData: TextCellViewData(title: "Bar")) + ]) + + let viewController = ConfigurableTableViewController(items: [firstSectionConfigurator, secondSectionConfigurator]) window = UIWindow(frame: UIScreen.mainScreen().bounds) window?.rootViewController = viewController diff --git a/Framework/CellConfigurator.swift b/Framework/CellConfigurator.swift index 4d0c359..5864896 100644 --- a/Framework/CellConfigurator.swift +++ b/Framework/CellConfigurator.swift @@ -8,6 +8,14 @@ import UIKit +struct SectionConfigurator { + var cellConfigurators: [CellConfiguratorType] + + init(cellConfigurators: [CellConfiguratorType]) { + self.cellConfigurators = cellConfigurators + } +} + protocol CellConfiguratorType { var reuseIdentifier: String { get } diff --git a/Framework/ConfigurableTableViewController.swift b/Framework/ConfigurableTableViewController.swift index 44db8ee..1030ef2 100644 --- a/Framework/ConfigurableTableViewController.swift +++ b/Framework/ConfigurableTableViewController.swift @@ -12,9 +12,9 @@ class ConfigurableTableViewController: UIViewController { weak var tableView: UITableView! - var items: [CellConfiguratorType] + var items: [SectionConfigurator] - init(items: [CellConfiguratorType]) { + init(items: [SectionConfigurator]) { self.items = items super.init(nibName: nil, bundle: nil) } @@ -26,7 +26,7 @@ class ConfigurableTableViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let tableView = UITableView(frame: view.bounds) + let tableView = UITableView(frame: view.bounds, style: .Grouped) tableView.tableFooterView = UIView() tableView.rowHeight = 100 view.addSubview(tableView) @@ -37,20 +37,27 @@ class ConfigurableTableViewController: UIViewController { } func registerCells() { - for cellConfigurator in items { - tableView.registerClass(cellConfigurator.cellClass, forCellReuseIdentifier: cellConfigurator.reuseIdentifier) + for sectionConfigurator in items { + for cellConfigurator in sectionConfigurator.cellConfigurators { + tableView.registerClass(cellConfigurator.cellClass, forCellReuseIdentifier: cellConfigurator.reuseIdentifier) + } } } } extension ConfigurableTableViewController: UITableViewDataSource { - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func numberOfSectionsInTableView(tableView: UITableView) -> Int { return items.count } + + func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return items[section].cellConfigurators.count + } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let cellConfigurator = items[indexPath.row] + let sectionConfigurator = items[indexPath.section] + let cellConfigurator = sectionConfigurator.cellConfigurators[indexPath.row] let cell = tableView.dequeueReusableCellWithIdentifier(cellConfigurator.reuseIdentifier, forIndexPath: indexPath) cellConfigurator.updateCell(cell) return cell