首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ContextMenuConfigurationForRowAt in iOS13

ContextMenuConfigurationForRowAt in iOS13
EN

Stack Overflow用户
提问于 2019-09-20 17:12:57
回答 1查看 3.9K关注 0票数 4

在WWDC19旺季( 更新iOS 13的UI )中的这个视频中,这个方法是创建一个上下文菜单,但是我在使用它时会遇到一个错误:

代码语言:javascript
复制
@available(iOS 13.0, *)
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    let actionProvider = (suggestedActions: [UIMenuElement])-> UIMenu? // in this line i got an error {
        let editMenu = UIMenu(title: "Edit...", children: [
        UIAction(title: "Copy") {},
        UIAction(title: "Duplicate") {}
        ])
        return UIMenu(children: [
        UIAction(title: "Share") {},
        editMenu,
        UIAction(title: "Delete", style: .destructive) {}
        ])
    }

    return UIContextMenuConfiguration(identifier: "unique-ID" as NSCopying,
                                      previewProvider: nil,
                                      actionProvider: actionProvider)
}

错误出现在行-> UIMenu?中,并显示为Expected type after '->'。有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-20 18:10:50

您有许多语法错误:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    let actionProvider: UIContextMenuActionProvider = { _ in
        let editMenu = UIMenu(title: "Edit...", children: [
            UIAction(title: "Copy") { _ in },
            UIAction(title: "Duplicate") { _ in }
        ])
        return UIMenu(title: "Title", children: [
            UIAction(title: "Share") { _ in },
            editMenu
        ])
    }

    return UIContextMenuConfiguration(identifier: "unique-ID" as NSCopying, previewProvider: nil, actionProvider: actionProvider)
}

请注意,一些API在WWDC之后发生了更改,您应该考虑更新它们,类似于上面的代码。您可以查看凯尔·巴希尔所写的全面的iOS上下文菜单指南

示例:

代码语言:javascript
复制
func makeContextMenu() -> UIMenu {
    let rename = UIAction(title: "Rename Pupper", image: UIImage(systemName: "square.and.pencil")) { action in
        // Show rename UI
    }

    // Here we specify the "destructive" attribute to show that it’s destructive in nature
    let delete = UIAction(title: "Delete Photo", image: UIImage(systemName: "trash"), attributes: .destructive) { action in
        // Delete this photo 
    }

    // The "title" will show up as an action for opening this menu
    let edit = UIMenu(title: "Edit...", children: [rename, delete])

    let share = UIAction(...)

    // Create our menu with both the edit menu and the share action
    return UIMenu(title: "Main Menu", children: [edit, share])
}

func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in

        // "puppers" is the array backing the collection view
        return self.makeContextMenu(for: self.puppers[indexPath.row])
    })
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58032652

复制
相关文章

相似问题

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