用于数组索引的索引集

用于数组索引的索引集

本文介绍了SwiftUI-用于数组索引的索引集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在NavigationView和列表中使用ForEach,并结合了以下功能,当用户使用.onDelete()删除行时,该功能如下所述.

I am using ForEach within a NavigationView and list combined with a function called when the user deletes a row using .onDelete() as per below.

struct PeriodListView: View {
@ObservedObject var theperiodlist = ThePeriodList()
@EnvironmentObject var theprofile: TheProfile

@State private var showingAddPeriod = false

var dateFormatter: DateFormatter {
    let formatter = DateFormatter()
    formatter.dateStyle = .long
    return formatter
}

var body: some View {
    NavigationView {
        List {
            ForEach(theperiodlist.periods) {period in
                PeriodRow(period: period)
            }
            .onDelete(perform: removePeriods)
        }
        .navigationBarTitle("Periods")
            .navigationBarItems(trailing:
                Button(action: {self.showingAddPeriod = true}) {
                    Image(systemName: "plus")
                }
            )
        .sheet(isPresented: $showingAddPeriod) {
            AddPeriod(theperiodlist: self.theperiodlist).environmentObject(self.theprofile)
        }
    }
}
func removePeriods(at offsets: IndexSet) {
    AdjustProfileRemove(period: theperiodlist.periods[XXX])
    theperiodlist.periods.remove(atOffsets: offsets)
}

我有一个单独的函数(AdjustProfileRemove(period)),我想使用删除的句点作为变量来调用它,例如我想在AdjustProfileRemove中找到XXX(句号:theperiodlist.periods [XXX]).有没有简单的方法可以做到这一点(我从IndexSet猜到)还是缺少一些基本知识?

I have a separate function (AdjustProfileRemove(period)) which I want to call with the removed period as the variable - e.g. I want to find XXX in AdjustProfileRemove(period: theperiodlist.periods[XXX]). Is there a simple way to do this (I am guessing from IndexSet) or am I missing something fundamental?

谢谢.

推荐答案

.onDelete被声明为

.onDelete is declared as

@inlinable public func onDelete(perform action: ((IndexSet) -> Void)?) -> some DynamicViewContent

IndexSet只是要删除的数组中元素的所有索引的Set.让我们尝试这个例子

IndexSet is simply Set of all indexes of the elements in the array to remove. Let try this example

var arr = ["A", "B", "C", "D", "E"]
let idxs = IndexSet([1, 3])

idxs.forEach { (i) in
    arr.remove(at: i)
}
print(arr)

所以现在的结果是

["A", "C", "D"]

.onDelete之所以使用IndexSet的原因是,可以选择List中的多个行进行删除操作.

The reason, why .onDelete use IndexSet is that more than one row in List could be selected for delete operation.

请小心!!查看生成的数组!实际上一一删除元素需要一些逻辑...

BE CAREFULL! see the resulting array! Actually removing elements one by one needs some logic ...

让我们尝试

var arr = ["A", "B", "C", "D", "E"]
let idxs = IndexSet([1, 3])

idxs.sorted(by: > ).forEach { (i) in
    arr.remove(at: i)
}
print(arr)

现在可以正常工作了,对吗?现在的结果是

it works now as you expected, is it? the result now is

["A", "C", "E"]

基于

theperiodlist.periods.remove(atOffsets: offsets)

看来, ThePeriodList 已具有内置功能和必需的功能.

it seems, that the ThePeriodList already has build-in function with required functionality.

在您的情况下,只需替换

in your case just replace

AdjustProfileRemove(period: theperiodlist.periods[XXX])

使用

offsets.sorted(by: > ).forEach { (i) in
    AdjustProfileRemove(period: theperiodlist.periods[i])
}

这篇关于SwiftUI-用于数组索引的索引集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:07