Skip to content
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

CellAction Sender #4

Merged
merged 1 commit into from
Jul 28, 2017
Merged
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
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)
}
}