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

时间:2018-07-20 09:56:57

标签: ios swift xcode swift3

enter image description here

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

  

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

2 个答案:

答案 0 :(得分:2)

此工具提示实际上有一个名称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()
    }
}

答案 1 :(得分:0)

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