我对iOS应用程序开发和SwiftUI相对较新,并且我目前正在开发一个允许用户按周创建时间表的应用程序。
我最近修改了该实体,使其也接受时间输入,以便列表根据它们的预定时间进行组织。快速浏览一下:
但是,当我单击每个项目时,它会将我发送到错误的事件。
在UIKit中,我注意到此问题普遍存在,原因是调用了多个视图,但是我看不到要在哪里显示多个视图。
主列表视图
List {
ForEach(self.Events) { event in
if("\(event.dotw ?? "Unknown")" == self.name) {
Button(action: {
self.isEvent.toggle()
}) {
HStack {
Text("\(event.name ?? "Unknown")")
.foregroundColor(Color.white)
Spacer()
Text("\(event.time ?? Date(), formatter: Self.taskDateFormat)")
}
}
.sheet(isPresented: self.$isEvent){
EventView(eventName: "\(event.name ?? "Unknown")", description: "\(event.descript ?? "Unknown")", eventTime: (event.time ?? Date())).environment(\.managedObjectContext, self.managedObjectContext)
}
.buttonStyle(ItemButton())
}
}.onDelete { indexSet in
let deleteItem = self.Events[indexSet.first!]
self.managedObjectContext.delete(deleteItem)
do{
try self.managedObjectContext.save()
} catch {
print(error)
}
}
Button(action: {
self.isPresented.toggle()
}) {
HStack {
Text("new")
Spacer()
}
}
.buttonStyle(NewItemButton())
.sheet(isPresented:$isPresented) {
NewEventView(dotw: self.name).environment(\.managedObjectContext, self.managedObjectContext)
}
}
.onAppear {
UITableView.appearance().separatorStyle = .none
}.onDisappear {
UITableView.appearance().separatorStyle = .singleLine
}
.navigationBarHidden(true)
.navigationBarTitle("")
.edgesIgnoringSafeArea(.top)
}
.edgesIgnoringSafeArea(.top)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
此代码:
.sheet(isPresented: self.$isEvent){
EventView(eventName: "\(event.name ?? "Unknown")", description: "\(event.descript ?? "Unknown")", eventTime: (event.time ?? Date())).environment(\.managedObjectContext, self.managedObjectContext)
}
我假设问题出在哪里,但是我没发现任何问题
事件视图
struct EventView: View {
var eventName: String
var description: String
var eventTime: Date
var body: some View {
VStack {
VStack {
Image(systemName: "chevron.compact.down")
.foregroundColor(.white)
.imageScale(.large)
.padding()
HStack {
Text(eventName)
.foregroundColor(.white)
.font(.largeTitle)
.padding(.top, 15)
.padding(.leading, 20)
Spacer()
}
.padding(.bottom)
.frame(maxWidth:.infinity)
}
.background(Color.gray)
.shadow(color: Color.black.opacity(0.2), radius: 7, x: 3, y: 3)
.shadow(color: Color.white.opacity(0.7), radius: 4, x: -5, y: -5)
VStack(alignment:.leading) {
HStack {
Text(description)
.padding(.leading, 20)
.padding(.top, 30)
.foregroundColor(.gray)
Spacer()
}
Spacer()
}
.frame(maxWidth:.infinity,maxHeight:.infinity)
}
.edgesIgnoringSafeArea(.top)
}
}
事件实体初始化器
import Foundation
import CoreData
import SwiftUI
public class Event:NSManagedObject,Identifiable {
@NSManaged public var name: String?
@NSManaged public var createdAt: Date?
@NSManaged public var descript: String?
@NSManaged public var dotw: String?
@NSManaged public var time: Date?
}
extension Event {
static func getAllEvents() -> NSFetchRequest<Event> {
let request: NSFetchRequest<Event> = NSFetchRequest<Event>(entityName: "Event")
let sortDescriptor = NSSortDescriptor(key: "time", ascending:true)
let sortDescriptor2 = NSSortDescriptor(key: "createdAt", ascending: true)
request.sortDescriptors = [sortDescriptor,sortDescriptor2]
return request
}
}
答案 0 :(得分:0)
您的活动可识别吗?这有助于SwiftUI知道哪个:
struct Events: Identifiable {
var id = UUID()
var name: String
}
还可能有助于将.id(event.id)
放入您的ForEach块中以在其中进行标识。