首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在执行上下文菜单操作时从TableViewCell获取数组的索引

如何在执行上下文菜单操作时从TableViewCell获取数组的索引
EN

Stack Overflow用户
提问于 2020-09-25 21:01:31
回答 1查看 39关注 0票数 0
代码语言:javascript
复制
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIContextMenuInteractionDelegate {

    @IBOutlet weak var plannerTableView: UITableView!

    ...  
  

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myPreparedTasks.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let task = self.myPreparedTasks[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskTableViewCell
        cell.taskId?.text = task.id

        let interaction = UIContextMenuInteraction(delegate: self)
        cell.addInteraction(interaction)

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }



    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
            return self.makeContextMenu()
        })
    }
    
    func makeContextMenu() -> UIMenu {
        let add = UIAction(title: "Neuer Task", image: UIImage(systemName: "plus.square")) { action in
            // Show the Task ID
        }

        return UIMenu(title: "Task", children: [add])
    }

}

iOS 13,Swift 5

嗨。这是我第一次在TableViewCell中实现上下文菜单。我做了上面的事情。而且看起来很棒。但是我无法从tableview内容中获得myPreparedTasks数组的ID。我怎么才能拿到这个身份证?请给我这个构造的答案。我在网络上看到了许多其他的构造,但我还不理解它。

非常感谢Jens

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-25 21:49:47

对于表视图中的上下文菜单,您必须实现委托方法

代码语言:javascript
复制
optional func tableView(_ tableView: UITableView, 
              contextMenuConfigurationForRowAt indexPath: IndexPath, 
              point: CGPoint) -> UIContextMenuConfiguration?

而不是将UIContextMenuInteraction添加到每个单元。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64064685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档