Skip to content

Commit

Permalink
Merge pull request #4 from Blackjacx/cell_action_sender
Browse files Browse the repository at this point in the history
CellAction Sender
  • Loading branch information
Blackjacx authored Jul 28, 2017
2 parents 9101b67 + 42e4606 commit 95450de
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Source/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public typealias CellAction = () -> Void
public typealias CellAction = (_ sender: Any) -> Void

public protocol Item {

Expand Down
10 changes: 0 additions & 10 deletions Source/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,3 @@ extension Source: UITableViewDataSource {
// optional public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
// optional public func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
}

extension Source: UITableViewDelegate {

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
let item = collection[indexPath]
item.action?()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MultiSectionViewController: UIViewController {

dataSource.collection = ItemCollection(with: sections)
table.dataSource = dataSource
table.delegate = dataSource
table.delegate = self

table.tableFooterView = UIView()
table.addMaximizedTo(view)
Expand All @@ -45,3 +45,13 @@ class MultiSectionViewController: UIViewController {
dataSource.useSectionIndexTitles = !dataSource.useSectionIndexTitles
}
}

extension MultiSectionViewController: UITableViewDelegate {

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
let item = dataSource.collection[indexPath]
item.action?(indexPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@ class SimpleExampleViewController: UIViewController {
super.viewDidLoad()

let items = [
MyItem(title: "Einstellungen", action:{ print("Einstellungen") }),
MyItem(title: "Hilfe", action:{ print("Hilfe") }),
MyItem(title: "Logout", action:{ print("Logout") })
MyItem(title: "Einstellungen", action:{ (sender) in print("Einstellungen") }),
MyItem(title: "Hilfe", action:{ (sender) in print("Hilfe") }),
MyItem(title: "Logout", action:{ (sender) in print("Logout") })
]

let section = MySection(items: items, headerTitle: nil, footerTitle: nil)
dataSource.collection = ItemCollection(with: [section])
table.dataSource = dataSource
table.delegate = dataSource

table.delegate = self
table.tableFooterView = UIView()
table.addMaximizedTo(view)
}
}

extension SimpleExampleViewController: UITableViewDelegate {

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
let item = dataSource.collection[indexPath]
item.action?(indexPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class SingleSectionViewController: UIViewController {
super.viewDidLoad()

var items = [
MyItem(title: "Einstellungen", action:{ print("Einstellungen") }),
MyItem(title: "Impressung", action:{ print("Impressum") }),
MyItem(title: "Empfehlen", action:{ print("Empfehlen") }),
MyItem(title: "Hilfe", action:{ print("Hilfe") }),
MyItem(title: "Logout", action:{ print("Logout") })
MyItem(title: "Einstellungen", action:{ (sender) in print("Einstellungen") }),
MyItem(title: "Impressung", action:{ (sender) in print("Impressum") }),
MyItem(title: "Empfehlen", action:{ (sender) in print("Empfehlen") }),
MyItem(title: "Hilfe", action:{ (sender) in print("Hilfe") }),
MyItem(title: "Logout", action:{ (sender) in print("Logout") })
]

// Changing the connected cell class for all items. Alternatively you can just create a new item and set another default cell type.
Expand All @@ -36,9 +36,19 @@ class SingleSectionViewController: UIViewController {
let section = MySection(items: items, headerTitle: nil, footerTitle: nil)
dataSource.collection = ItemCollection(with: [section])
table.dataSource = dataSource
table.delegate = dataSource
table.delegate = self

table.tableFooterView = UIView()
table.addMaximizedTo(view)
}
}

extension SingleSectionViewController: UITableViewDelegate {

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
let item = dataSource.collection[indexPath]
item.action?(indexPath)
}
}

0 comments on commit 95450de

Please sign in to comment.