TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | Apache 2 |
ReleasedLast Release | Mar 2018 |
SPMSupports SPM | ✗ |
Maintained by Dan Loewenherz.
TableViewPopoverPresenting is a simple, drop-in protocol that gives your table views the power to display presented view controllers in popovers in reaction to a cell tap. It overrides the standard table view tap handler only for the cells which you define it for, and everything else falls back to your existing tableView(_:didSelectRowAt:)
implementation.
In the good ole’ days, in order to present popovers over your table view cells, you had to do a few things:
UITapGestureRecognizer
.UITableView
.UIViewController
conform to UIGestureRecognizerDelegate
.UITableView
touch handlers for tableView(_:didSelectRowAt:)
.Each one of these items requires going through StackOverflow posts with a fine-toothed comb to make sure you’re doing everything the “right” way. Obviously, that’s lame. You shouldn’t need to do all of this work for something which should be simple. And that’s where TableViewPopoverPresenting comes in.
TableViewPopoverPresenting is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "TableViewPopoverPresenting"
Install TableViewPopoverPresenting through Cocoapods by adding the following line to your Podfile.
pod "TableViewPopoverPresenting"
Import TableViewPopoverPresenting
in the file with your table view controller.
import TableViewPopoverPresenting
Call initializeTableViewPopover
in viewDidLoad
:
func viewDidLoad() {
super.viewDidLoad()
# ...
initializeTableViewPopover()
# ...
}
Implement viewController(forPopoverAt:)
to define which view controllers to show at which index paths. E.g.:
func viewController(forPopoverAt: IndexPath) -> UIViewController? {
if indexPath.row == 0 {
let actionSheet = UIAlertController(title: "Important Question", message: "What's your favorite color?", preferredStyle: .ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Red", style: .Default) { _ in })
actionSheet.addAction(UIAlertAction(title: "Green", style: .Default) { _ in })
actionSheet.addAction(UIAlertAction(title: "Blue", style: .Default) { _ in })
return actionSheet
} else {
return nil
}
}
And…that’s it. You’re done. A nice action sheet will appear when a tap is detected on the first row of any section. Easy, right?
None.
Dan Loewenherz, [email protected]
TableViewPopoverPresenting is available under the Apache 2.0 license. See the LICENSE file for more info.
Donations help support our open source efforts.