Ios 如何基于带有日期的对象数组创建多个节?
我想根据项目列表的日期字段创建一个包含多个节标题和项目的列表。以编程方式执行此操作的最佳方法是什么Ios 如何基于带有日期的对象数组创建多个节?,ios,swift,swiftui,Ios,Swift,Swiftui,我想根据项目列表的日期字段创建一个包含多个节标题和项目的列表。以编程方式执行此操作的最佳方法是什么 var registrants:[Registration] = [Registration(name:"Joe", date:12/15/2020),Registration(name:"Billy", date:11/12/2020),Registration(name:"Cameron", date:11/10/2020)] 通常
var registrants:[Registration] = [Registration(name:"Joe", date:12/15/2020),Registration(name:"Billy", date:11/12/2020),Registration(name:"Cameron", date:11/10/2020)]
通常,我会硬编码并仅对一个特定部分中的项使用foreach循环,如下所示:
List {
Section(header: Text("December 2020")) {
ForEach(...)
}
Section(header: Text("November 2020")) {
ForEach(...)
}
Section(header: Text("October 2020")) {
ForEach(...)
}
}
我正在寻找一种更智能的方法来实现这一点…在ForEach中使用您的数据应该是可识别的,或者应该有某种方法可以指定id
struct ContentView: View {
var registrants:[Registration] = [Registration(name:"Joe", date:"12/15/2020"),Registration(name:"Billy", date:"11/12/2020"),Registration(name:"Cameron", date:"11/10/2020")]
var body: some View {
List {
ForEach(registrants) { (registration) in //<=Here
Section(header: Text(registration.date)) {
ForEach(0 ..< 5) { item in
Text("item \(item)")
}
}
}
}
}
}
struct Registration : Identifiable{ //<= here
var id = UUID()
var name: String
var date: String
}
struct ContentView:View{
风险值登记人:[登记]=[登记(姓名:“Joe”,日期:“2020年12月15日”)、登记(姓名:“Billy”,日期:“2020年11月12日”)、登记(姓名:“Cameron”,日期:“2020年11月10日”)]
var body:一些观点{
名单{
ForEach(注册人){(注册)in//要在ForEach中使用,您的数据应该是可识别的,或者应该有某种方式可以指定id
struct ContentView: View {
var registrants:[Registration] = [Registration(name:"Joe", date:"12/15/2020"),Registration(name:"Billy", date:"11/12/2020"),Registration(name:"Cameron", date:"11/10/2020")]
var body: some View {
List {
ForEach(registrants) { (registration) in //<=Here
Section(header: Text(registration.date)) {
ForEach(0 ..< 5) { item in
Text("item \(item)")
}
}
}
}
}
}
struct Registration : Identifiable{ //<= here
var id = UUID()
var name: String
var date: String
}
struct ContentView:View{
风险值登记人:[登记]=[登记(姓名:“Joe”,日期:“2020年12月15日”)、登记(姓名:“Billy”,日期:“2020年11月12日”)、登记(姓名:“Cameron”,日期:“2020年11月10日”)]
var body:一些观点{
名单{
ForEach(注册人){(注册人)//将这些部分用ForEach too括起来这能回答你的问题吗?这能回答你的问题吗?将这些部分用ForEach too括起来这能回答你的问题吗?这能回答你的问题吗?