如何在表格视图的 didSelect 上显示弹出框

Posted

技术标签:

【中文标题】如何在表格视图的 didSelect 上显示弹出框【英文标题】:How to show popover on didSelect of table view 【发布时间】:2018-07-20 09:56:57 【问题描述】:

我想在我的 tableview 上实现这种类型的视图并选择。这是一个使用swift编程的ios项目..效果类似于whatsapp ios app...

任何人都可以对如何在表格视图上完成此操作有任何建议吗 细胞?

【问题讨论】:

您使用的是哪个弹出框? 只需将发件人传递给 didSelect 上的弹出框 我没有使用任何东西......我想实现它! 通过 pod 安装任何弹出框,并在 didSelect 上将发件人传递给弹出框。它会工作 你已经通过 cocoapods 使用了第三方,你可以在 github 上使用。 【参考方案1】:

这个工具提示实际上有一个名字,UIMenuController,可以自定义并在你自己的应用中使用:

class ResponsiveView: UIView 
    override var canBecomeFirstResponder: Bool 
        return true
    


class ViewController: UIViewController 

    var responsiveView: ResponsiveView!

    override func viewDidLoad() 
        super.viewDidLoad()

        responsiveView = ResponsiveView()

        // Add our responsive view to a super view
        responsiveView.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
        responsiveView.center = self.view.center
        responsiveView.backgroundColor = UIColor(red: 124.0/255.0, green: 112.0/255.0, blue: 255.0/255.0, alpha: 1.0)
        responsiveView.layer.cornerRadius = 4;
        responsiveView.layer.masksToBounds = true
        self.view.addSubview(responsiveView)

        // Add a long press gesture recognizer to our responsive view
        responsiveView.isUserInteractionEnabled = true
        let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(longPressHandler))
        longPressGR.minimumPressDuration = 0.3 // how long before menu pops up
        responsiveView.addGestureRecognizer(longPressGR)
    

    @objc func longPressHandler(sender: UILongPressGestureRecognizer) 
        guard sender.state == .began,
            let senderView = sender.view,
            let superView = sender.view?.superview
            else  return 

        // Make responsiveView the window's first responder
        senderView.becomeFirstResponder()

        // Set up the shared UIMenuController
        let saveMenuItem = UIMenuItem(title: "Save", action: #selector(saveTapped))
        let deleteMenuItem = UIMenuItem(title: "Delete", action: #selector(deleteTapped))
        UIMenuController.shared.menuItems = [saveMenuItem, deleteMenuItem]

        // Tell the menu controller the first responder's frame and its super view
        UIMenuController.shared.setTargetRect(senderView.frame, in: superView)

        // Animate the menu onto view
        UIMenuController.shared.setMenuVisible(true, animated: true)
    

    @objc func saveTapped() 
        print("save tapped")
        // ...
        // This would be a good place to optionally resign
        // responsiveView's first responder status if you need to
        responsiveView.resignFirstResponder()
    

    @objc func deleteTapped() 
        print("delete tapped")
        // ...
        responsiveView.resignFirstResponder()
    

【讨论】:

怎么用?@Harshal 我不知道他想要什么。但它看起来不错@RahulSinghaRoy 你应该试试。 @RahulSinghaRoy,请试试这个存储库github.com/harshal-wani/UITableViewCell-Tooltip【参考方案2】:
func menuPopup(index:IndexPath)
 var menuController = UIMenuController.shared
        let actionOneMenuItem = UIMenuItem(title: "Action1", action: #selector(actionOne))
        let actionTwoMenuItem = UIMenuItem(title: "Action1", action: #selector(actionTwo))
        let actionThreeMenuItem = UIMenuItem(title: "Action3", action: #selector(actionThree))
        menuController.isMenuVisible = true
        menuController.arrowDirection = .default
        let rect = contactsTableView.rectForRow(at: index)
        menuController.setTargetRect(rect, in: contactsTableView)
        menuController.menuItems = [actionOneMenuItem, actionTwoMenuItem,actionThreeMenuItem]
        menuController.setMenuVisible(true, animated: true)
    

    @objc func actionOne()

    
    @objc func actionTwo()

    
    @objc func actionThree()

    

    override var canBecomeFirstResponder: Bool
        return true
    

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        contactsTableView.deselectRow(at: indexPath, animated: true)
        tableView.becomeFirstResponder()
        menuPopup(index: indexPath)
    

【讨论】:

以上是关于如何在表格视图的 didSelect 上显示弹出框的主要内容,如果未能解决你的问题,请参考以下文章

IOS:用表格视图填充弹出框

通过弹出框 didselcet 关闭模态视图

选择器作为弹出框内的输入视图

弹出框显示在表格视图中长用户输入表单的顶部

在表格视图中选择项目后无法关闭弹出框

弹出框控制器无法正确显示视图