TableViewPopoverPresenting 3.1.1

TableViewPopoverPresenting 3.1.1

测试已测试
语言语言 SwiftSwift
许可证 Apache 2
发布上次发布2018年3月
SPM支持 SPM

Dan Loewenherz maintain.



  • Dan Loewenherz

TableViewPopoverPresenting




TableViewPopoverPresenting 是一个简单、即插即用的协议,它为您的表格视图提供了在响应单元格点击时显示弹出视图控制器的能力。它仅对您定义的单元格覆盖了标准的表格视图点击处理器,其他所有内容都回退到您现有的 tableView(_:didSelectRowAt:) 实现中。

为何存在

在美好往日,为了在您的表格视图单元格上显示弹出视图,您需要做几件事情

  1. 实例化一个 UITapGestureRecognizer
  2. 将其添加到您的 UITableView
  3. 让您的 UIViewController 符合 UIGestureRecognizerDelegate
  4. 创建一个处理点击的方法。
  5. 确保点击手势识别器不会覆盖标准 UITableView 的触摸处理器 tableView(_:didSelectRowAt:)
  6. 确定用户点击的位置。
  7. 检索用户对应的点击位置的索引路径。
  8. 确保您实际上想在那个索引路径显示视图控制器。
  9. 决定显示哪个视图控制器。
  10. 最终,在用户点击的位置呈现视图控制器。

这些都需要通过 StackOverflow 帖子进行细致检查,以确保您正在按照“正确”的方式进行。显然,这是很愚蠢的。您不应该需要为如此简单的事情做所有这些工作。这正是 TableViewPopoverPresenting 介入的地方。

安装

TableViewPopoverPresenting 通过 CocoaPods 提供。要安装它,只需将下面的行添加到您的 Podfile 中

pod "TableViewPopoverPresenting"

使用

  1. 通过将以下行添加到您的 Podfile 中,使用 Cocoapods 安装 TableViewPopoverPresenting。

    pod "TableViewPopoverPresenting"
  2. 在包含表格视图控制器的文件中导入 TableViewPopoverPresenting

    import TableViewPopoverPresenting

现在,对于每个视图控制器,请执行以下操作

  1. viewDidLoad 中调用 initializeTableViewPopover

    func viewDidLoad() {
        super.viewDidLoad()
    
        # ...
    
        initializeTableViewPopover()
    
        # ...
    }
  2. 实现 viewController(forPopoverAt:) 以在哪些索引路径显示哪个视图控制器。例如。

    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
        }
    }

好了……这样就完成了。当在任何部分的第一个行上检测到点击时,将出现一个 nice action sheet。简单,对吧?

要求

无。

作者

Dan Loewenherz,[email protected]

许可证

TableViewPopoverPresenting 在 Apache 2.0 许可证下提供。有关更多信息,请参阅 LICENSE 文件。

捐赠

捐赠有助于支持我们的开源努力。

立即捐赠