简体   繁体   English

点击时从列表中删除 SwiftUI

[英]Delete From List When Tapped SwiftUI

I want to delete an item from the list when the item is tapped.点击项目时,我想从列表中删除项目。 When I delete one of the cells all the rest of the cell tap indicators are showing that they are tapped too.当我删除其中一个单元格时,单元格抽头指示器的所有 rest 都显示它们也被点击了。

Plus it does not delete it just makes it temporarily invisible.另外,它不会删除它只是使其暂时不可见。 When I reload the page deleted cells are again in the memory and visible again.当我重新加载页面时,删除的单元格再次位于 memory 中并且再次可见。

List{
                ForEach(list.reminders, id: \.self) { reminder in
                    Button(action: {
                        self.isSelected.toggle()
                        self.delete(at: list.reminders.firstIndex(where: {$0.hashValue == reminder.hashValue})!)
                    }, label: {
                        HStack{
                            Image(systemName: isSelected ? "largecircle.fill.circle" : "circle")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .foregroundColor((isSelected ? Color(list.color) : .gray))
                                .frame(width: 22, height: 22, alignment: .trailing)
                                .cornerRadius(22)
                            ReminderCell(reminder: reminder)
                        }
                    })
                    .padding(.bottom)
                }.onDelete(perform: deleteReminder)
            }

func deleteReminder(at offsets: IndexSet) {
    list.reminders.remove(atOffsets: offsets)
}
func delete(at index: Int) {
    list.reminders.remove(at: index)
}

First, If you want to make the function in .onTapGesture {} work, you have to put it outside Button label, because when you tap it, the function in Button action will be triggered.首先,如果你想让.onTapGesture {}中的 function 工作,你必须把它放在 Button label 之外,因为当你点击它时,ZC1C425268E68385D1AB5074C14Z 的动作会被触发。

Second, if you want to get index of element in ForEach, you have to add indices .其次,如果要获取 ForEach 中元素的索引,则必须添加indices Check my code below, because you don't post your code completely, I can't test my code exactly.在下面检查我的代码,因为您没有完全发布您的代码,我无法准确测试我的代码。

        List {
            ForEach(list.reminders.indices, id: \.self) { index in
                HStack {
                    Button(action: {
                        self.isSelected.toggle()
                    }, label: {
                        Image(systemName: isSelected ? "largecircle.fill.circle" : "circle")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .foregroundColor((isSelected ? Color(list.color) : .gray))
                            .frame(width: 22, height: 22, alignment: .trailing)
                            .cornerRadius(22)
                    })
                    Button(action: {
                        deleteReminder(at: IndexSet(integer: index))
                    }) {
                        ReminderCell(reminder: list.reminders[index])
                    }
                }
                .padding(.bottom)
            }.onDelete(perform: deleteReminder)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM