Search code examples
iosobjective-cuitableviewcell

iOS - How to add bubble with text/buttons over cell in UITableView?


I really have no idea how to call this feature, music app had it up to 8.4, it looks like on the screenshot. I want to implement it in my app so when user presses the cell the "bubble" with 2 option buttons shows up. I am interested in how to make it happen in Obj-C but I'm sure people will apreciate the answer written in Swift. Thanks Screenshot


Solution

  • I'm doing exactly the same thing as what you want. To do so, you have to create your own view controller, not UIMenuItem. The screen capture is as follows.

    enter image description here

    What I'm doing is to create a viewController as popup (PopoverMenuController), and adding a tableView as a subView for menu. In the same way, you can add whatever UI controls you want instead of tableView.

    Here is how you use my PopoverMenuController.

    var myPopupMenu: PopoverMenuController!
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // make cell fully visible
        let tableCell = tableView.cellForRow(at: indexPath)!
        tableView.scrollRectToVisible(tableView.rectForRow(at: indexPath), animated: false)
        // pop up menu
        myPopupMenu = PopoverMenuController()
        myPopupMenu.sourceView = tableCell
        var rect = tableCell.bounds
        // I'm adjusting popup menu position slightly (this is optional)
        rect.origin.y = rect.size.height/3.0
        rect.size.height = rect.size.height / 2.0
        myPopupMenu.sourceRect = rect
        myPopupMenu.addAction(PopMenuAction(textLabel: "MyMenu-1", accessoryType: .none, handler: myMenuHandler1))
        myPopupMenu.addAction(PopMenuAction(textLabel: "MyMenu-2", accessoryType: .none, handler: myMenuHandler2))
        present(myPopupMenu, animated: true, completion: nil)
    }
    
    func myMenuHandler1(_ action: PopMenuAction?) -> Bool {
        // do some work...
        return false
    }
    
    func myMenuHandler2(_ action: PopMenuAction?) -> Bool {
        // do some work...
        return false
    }
    

    To transltae to Obj-C should not be a big effort. I put souce code of PopoverMenuController.swift here. This controller is self-contained and it has a description on how to use.

    Hope this helps.