gpt4 book ai didi

swift - iOS13 中的 ContextMenuConfigurationForRowAt

转载 作者:行者123 更新时间:2023-12-01 12:44:50 27 4
gpt4 key购买 nike

在 WWDC19 季的这段视频中,Modernizing Your UI for iOS 13 ,这个方法是创建一个上下文菜单,但是使用的时候报错:

@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 '->'。谁能帮我解决一下?

最佳答案

你有很多语法错误:

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 之后发生了变化,您应该考虑更新它们,类似于上面的代码。您可以查看全面的Guide to iOS Context Menus由 Kyle Bashour 撰写。

例子:

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

关于swift - iOS13 中的 ContextMenuConfigurationForRowAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com