-
Notifications
You must be signed in to change notification settings - Fork 87
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
Add ability to receive UITableViewDelegate & UIScrollViewDelegate messages from UITableView #119
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Pod::Spec.new do |spec| | ||
spec.name = 'Static' | ||
spec.version = '2.2.0' | ||
spec.version = '2.3.0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a breaking change? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nah, bro. I don't know what Venmo uses for CocoaPods, but I assumed Semantic Versioning: https://semver.org/
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahhh, you right you right. |
||
spec.summary = 'Simple static table views for iOS in Swift.' | ||
spec.description = 'Static provides simple static table views for iOS in Swift.' | ||
spec.homepage = 'https://github.com/venmo/static' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,12 +48,13 @@ public class DataSource: NSObject { | |
|
||
// MARK: - Initializers | ||
|
||
/// Initialize with optional `tableView` and `sections`. | ||
public init(tableView: UITableView? = nil, sections: [Section]? = nil) { | ||
/// Initialize with optional `tableView`, `sections` and `tableViewDelegate`. | ||
public init(tableView: UITableView? = nil, sections: [Section]? = nil, tableViewDelegate: UITableViewDelegate? = nil) { | ||
assert(Thread.isMainThread, "You must access Static.DataSource from the main thread.") | ||
|
||
self.tableView = tableView | ||
self.sections = sections ?? [] | ||
self.tableViewDelegate = tableViewDelegate | ||
|
||
super.init() | ||
|
||
|
@@ -73,6 +74,23 @@ public class DataSource: NSObject { | |
return row(at: indexPath) | ||
} | ||
|
||
// MARK: - Forwarding UITableViewDelegate messages | ||
|
||
/// If you have a use for `UITableViewDelegate` or `UIScrollViewDelegate` messages, you can use this property to receive those messages. `DataSource` needs to be the `UITableView` instance's true `delegate`, but will forward messages to this property. | ||
/// You must pass this in the `init` function. | ||
weak public private(set) var tableViewDelegate: UITableViewDelegate? | ||
|
||
override public func forwardingTarget(for aSelector: Selector!) -> Any? { | ||
if let forwardDelegate = tableViewDelegate, forwardDelegate.responds(to: aSelector) { | ||
return forwardDelegate | ||
} else { | ||
return super.forwardingTarget(for: aSelector) | ||
} | ||
} | ||
|
||
override public func responds(to aSelector: Selector!) -> Bool { | ||
return super.responds(to: aSelector) || tableViewDelegate?.responds(to: aSelector) == true | ||
} | ||
|
||
// MARK: - Private | ||
|
||
|
@@ -275,12 +293,16 @@ extension DataSource: UITableViewDelegate { | |
if let row = row(at: indexPath) { | ||
row.selection?() | ||
} | ||
|
||
tableViewDelegate?.tableView?(tableView, didSelectRowAt: indexPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this needed if we have the forwarding target?.. wouldn't it send this there anyway?.. I may be misunderstanding forwardingTarget... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Messages only get forwarded to the forwardingTarget if this object doesn't implement. |
||
} | ||
|
||
public func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { | ||
if let row = row(at: indexPath) { | ||
row.accessory.selection?() | ||
} | ||
|
||
tableViewDelegate?.tableView?(tableView, accessoryButtonTappedForRowWith: indexPath) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see this item being useful...
But didSelect is covered as part of Static closure handling, and scrollViewWillBeginDragging can be observed with a observer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functions here are not comprehensive regarding what methods will get forwarded, they are just meant to demonstrate this functionality. They all get forwarded, including any potential future
UIScrollViewDelegate
functions. The only ones that do not are the ones that Static handles completely where it doesn't make sense to forward.I don't think all
UIScrollViewDelegate
methods can be handled with observers. Developers are more familiar withUIScrollViewDelegate
&UITableViewDelegate
methods than they are with observing those same changes. This is why Static has at least 3 tickets around requesting this functionality. If the code only forwarded the calls that can't be done with observers, that would obviously create more confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
understood