我正在为我们公司的工程师开发一个应用程序(使用 Xcode 11.3.1,目标设备:iPad)来报告他们所做的工作。应用程序的一部分需要是他们使用过的部分的可编辑列表。
我已经在一个简单的“人员列表”测试项目(下面的完整项目代码)中复制了我试图实现的机制(观察对象/@Binding 等)。
我仍在努力学习 SWiftUI,所以我可能在我的代码中做了一些愚蠢的事情。
这里的目标是创建一个带有可编辑字段的动态列表。预览代码时,它似乎可以完美运行,但是,在删除元素后,事情就开始出错了。(删除最后一个元素会导致“致命错误:索引超出范围”。
如果在删除一些元素后添加新元素,则新元素的文本字段为空白且不可编辑。
我非常感谢任何人可以提供的任何帮助。
import SwiftUI
struct EditView: View {
@Binding var person:Person
var body: some View {
HStack{
Group{
TextField("name1", text: $person.name1)
TextField("name2", text: $person.name2)
}.frame(width:150)
.font(.headline)
.padding(.all, 3)
.overlay(RoundedRectangle(cornerRadius: 4).stroke(Color.blue, lineWidth: 1))
}
}
}
struct Person:Identifiable, Equatable{
var id:UUID
var name1:String
var name2:String
}
class PersonList: ObservableObject {
@Published var individuals = [Person]()// Array of Person structs
}
struct ContentView: View {
@ObservedObject var people = PersonList()// people.individuals = [Person] array
@State private var edName1:String = "" //temporary storage for adding new member
@State private var edName2:String = "" //temporary storage for adding new member
var elementCount:Int{
let c = people.individuals.count
return c
}
// arrays for testing - adds random names from these (if input field '1st name' is empty)...
var firstNames = ["Nick","Hermes","John","Hattie","Nicola","Alan", "Dwight", "Richard"]
var surnames = ["Fury","Smith","Jones","Hargreaves","Bennylinch", "Davidson","Lucas","Partridge"]
var body: some View {
NavigationView{
VStack{
HStack{
Text("Add person:")
.padding(.all, 5)
.frame(alignment: .leading)
TextField("1st name", text: $edName1)
.frame(width:150)
.padding(.all, 5)
.overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.blue, lineWidth: 2))
TextField("2nd name", text: $edName2)
.frame(width:150)
.padding(.all, 5)
.overlay(RoundedRectangle(cornerRadius: 8)
.stroke(Color.blue, lineWidth: 2))
// Button...
Image(systemName: "plus.square")
.font(.title)
.foregroundColor(.orange)
.onTapGesture {
if self.edName1 == ""{
self.edName1 = self.firstNames.randomElement() ?? "⁉️"
self.edName2 = self.surnames.randomElement() ?? "⁉️"
}
self.people.individuals.append(Person(id: UUID(), name1: self.edName1, name2: self.edName2))
self.edName1 = ""
self.edName2 = ""
print("Element count: \(self.elementCount)")
}
Spacer()
// Button...sort
Image(systemName: "arrow.up.arrow.down.square")
.font(.title)
.padding(.all,4)
.foregroundColor(.blue)
.onTapGesture {
self.people.individuals.sort{ // sort list alphabetically by name2
$0.name2 < $1.name2
}
}
// Button...reverse order
Image(systemName: "arrow.uturn.up.square")
.font(.title)
.padding(.all,8)
.foregroundColor(.blue)
.onTapGesture {
self.people.individuals.reverse()
}
}.padding(.all,8)
.overlay(RoundedRectangle(cornerRadius: 12)
.stroke(Color.orange, lineWidth: 2))
List{
ForEach(people.individuals){individual in
HStack{
EditView(person: self.$people.individuals[self.people.individuals.firstIndex(of:individual)!])
Text("\(individual.name1) \(individual.name2)")
.frame(width: 200, alignment: .leading)
.padding(.all, 3)
Text("\(self.people.individuals.firstIndex(of: individual)!)")
Spacer()
Image(systemName: "xmark.circle.fill").font(.title).foregroundColor(.red)//❌
.onTapGesture {
let deletedElement = self.people.individuals.remove(at: self.people.individuals.firstIndex(of: individual)!)
print("Deleted element:\(deletedElement) Element count: \(self.elementCount)")
}
}
}//.onDelete(perform: deleteRow)// an alternative to the red xmark circle
}
}.navigationBarTitle("People List (\(elementCount))")
}.navigationViewStyle(StackNavigationViewStyle())
}
func deleteRow(at offsets: IndexSet){
self.people.individuals.remove(atOffsets: offsets)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environment(\.colorScheme, .dark)
}
}