Skip to content

Add sections support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions ExampleApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextTableViewCell>(viewData: TextCellViewData(title: "Foo")),
CellConfigurator<ImageTableViewCell>(viewData: ImageCellViewData(image: UIImage(named: "Apple")!)),
CellConfigurator<ImageTableViewCell>(viewData: ImageCellViewData(image: UIImage(named: "Apple")!))
])

let secondSectionConfigurator = SectionConfigurator(cellConfigurators: [
CellConfigurator<ImageTableViewCell>(viewData: ImageCellViewData(image: UIImage(named: "Google")!)),
CellConfigurator<TextTableViewCell>(viewData: TextCellViewData(title: "Bar")),
])
CellConfigurator<TextTableViewCell>(viewData: TextCellViewData(title: "Bar"))
])

let viewController = ConfigurableTableViewController(items: [firstSectionConfigurator, secondSectionConfigurator])

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = viewController
Expand Down
8 changes: 8 additions & 0 deletions Framework/CellConfigurator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

import UIKit

struct SectionConfigurator {
var cellConfigurators: [CellConfiguratorType]

init(cellConfigurators: [CellConfiguratorType]) {
self.cellConfigurators = cellConfigurators
}
}

protocol CellConfiguratorType {

var reuseIdentifier: String { get }
Expand Down
21 changes: 14 additions & 7 deletions Framework/ConfigurableTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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
Expand Down