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

拟墨画扇 提交于 2019-12-24 07:09:37

问题


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


回答1:


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.

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.




回答2:


Look up how to implement UIMenuController on a UITableViewCell

How to show a custom UIMenuItem for a UITableViewCell



来源:https://stackoverflow.com/questions/41086363/ios-how-to-add-bubble-with-text-buttons-over-cell-in-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!