swiftui - SwiftUI - removeAll 的错误索引(其中:)
问题描述
我想让用户删除数组中的图像。按下图像时,会显示一个操作表,以便用户确认删除图像。问题是它删除了错误的图像。它总是删除数组的第一个图像。
@State var pickerResult: [SImage] = []
...
ScrollView(.horizontal, showsIndicators: true){
HStack{
ForEach(pickerResult) { simage in
Image(uiImage: simage.image)
.onTapGesture() {
imageActionSheetIsPresented = true
// This will work: self.pickerResult.removeAll(where: {$0.image == simage.image})
}
.actionSheet(isPresented: $imageActionSheetIsPresented) {
ActionSheet(title: Text("Do you want to remove the image?"), buttons: [
.default(Text("Remove image")){
self.pickerResult.removeAll(where: {$0.image == simage.image})
print(simage.id)
// Returns id of the first image in the array
},
.cancel()
])
}
}
}
}
正如您在代码中看到的,跳过确认并让用户使用 onTap 删除图像就可以了。使用 ActionSheet 时,“图像”始终是数组中的第一项。
这是SImage:
struct SImage: Identifiable{
var id = UUID()
var image: UIImage
}
解决方案
推荐阅读
- flutter - ImageIcon 被压扁的颤动
- javascript - 我的应用程序运行良好,但现在显示错误 [未处理的承诺拒绝:类型错误:传播不可迭代实例的无效尝试
- java - 我可以将硬盘中的 java 类文件导入 Android Studio 中的项目吗?
- react-native - React Native Facebook 登录库(react-native-fbsdk) - 如何更改徽标?
- discord.py - 如果为发送命令寻找特定(服务器/公会),则执行“其他”
- c++ - 如果用户尝试在 C++ 中输入意外类型的信息,如何处理异常
- python - WinAppDriver,Python,如何在弹出窗口中选择元素
- java - Spring Security 如何允许某些特定路径?
- python - Python validate_email 类型错误
- c# - Blazor CSS 隔离不会覆盖 Bootstrap 样式