视图不显示列表中的文本

问题描述 投票:0回答:1

我有一个 CalendarView,它将一组约会传递给 DayView。约会似乎顺利进行,我已经验证视图代码正在运行,但没有显示任何内容。我尝试清理构建文件夹。这里有什么想法吗?

不显示的代码是:

           List {
                ForEach(appointments) { appointment in
                    DayCellView(appointment: appointment)
                }
            }

整个视图是:

import SwiftUI

struct DayView: View {
    var appointments: [Appointment] = []
    
    @State var dates = [
            Date(),
            Calendar.current.date(byAdding: .minute, value: 30, to: Date()),
            Calendar.current.date(byAdding: .hour, value: 60, to: Date()),
            Calendar.current.date(byAdding: .hour, value: 90, to: Date()),
            Calendar.current.date(byAdding: .hour, value: 120, to: Date())
        ]
        .compactMap { $0 }
    
    @State var selectedDate: Date?
    
    @State var currentDate: Date
    
    var body: some View {
        ScrollView {
            
            List {
                ForEach(appointments) { appointment in
                    DayCellView(appointment: appointment)
                }
            }
            
            VStack {
                Text(currentDate.fullMonthDayYearFormat())
                
                Divider()
                    .padding(.vertical)
                
                Text("Select a Time")
                    .font(.largeTitle)
                    .bold()
                
                Text("Duration: 15 minutes")
                
                ForEach(dates, id: \.self) { date in
                    HStack {
                        Button {
                            withAnimation {
                                selectedDate = date
                            }
                        } label: {
                           
                            Text( (date.timeFromDate()))
                                .bold()
                                .padding()
                                .frame(maxWidth: .infinity)
                                .foregroundColor(selectedDate == date ? .white : .blue)
                                .background(ZStack {
                                    if selectedDate == date {
                                        RoundedRectangle(cornerRadius: 10).foregroundColor(.gray)
                                    } else {
                                        RoundedRectangle(cornerRadius: 10).stroke()
                                    }
                                }
                                )
                    }
                        if selectedDate == date {
                            NavigationLink {
                                if let resident = NetworkViewModel.resident {
                                    BookingView( appointment: Appointment(residentID: resident.id!, firstName: "", lastName: "", phoneNumber: "", date: selectedDate!, time: selectedDate!.timeFromDate(), description: "", house: "", residentAddress: "", addressOfAppt: "", hasRideScheduled: false))
                                }
                                
                            } label: {
                               Text("Next")
                                    .bold()
                                    .padding()
                                    .frame(maxWidth: .infinity)
                                    .foregroundColor(.white)
                                    .background(
                                    RoundedRectangle(cornerRadius: 10)
                                        .foregroundColor(.blue)
                                    )
                            }
                        }
                    }
                }
                .padding(.horizontal)
            }
        }
        .navigationTitle(currentDate.dayOfWeek())
        .navigationBarTitleDisplayMode(.inline)
        .onAppear {
            dates = timesWith30MinuteIncrements(for: currentDate)
        }
    }
    
    func timesWith30MinuteIncrements(for date: Date) -> [Date] {
        let calendar = Calendar.current
        let startDate = calendar.date(bySettingHour: 8, minute: 0, second: 0, of: date)!
        let endDate = calendar.date(bySettingHour: 18, minute: 0, second: 0, of: date)!
        
        var times: [Date] = []
        var currentDate = startDate
        while currentDate <= endDate {
            times.append(currentDate)
            guard let newDate = calendar.date(byAdding: .minute, value: 15, to: currentDate) else { break }
            currentDate = newDate
        }
        
        return times
    }
}

这是 DayCellView:

import SwiftUI

struct DayCellView: View {
    var appointment: Appointment
    
    var body: some View {
        NavigationLink(destination: AppointmentDetailView(appointment: appointment)
        ) {
            var dateString = appointment.date.formatted(date: .complete, time: .omitted)
            
            VStack(alignment: .leading) {
                
                
                HStack() {
                    Text("Resident Name:")
                    Text(appointment.firstName + " " + appointment.lastName)
                }
                .font(.headline)
                
                HStack {
                    Text("Appointment Date:")
                    Text(dateString)
                }
                
                HStack {
                    Text("Appointment Time:")
                    Text(appointment.time)
                }
                
                HStack {
                    Text("Description: ")
                    Text(appointment.description)
                }
                
                HStack {
                    Text("Is Appointment Confirmed? ")
                    
                    if appointment.hasRideScheduled {
                        Text("Confirmed")
                    } else {
                        Text("Not Confirmed Yet")
                    }
                }
            }
            .frame(maxWidth: .infinity)
            .background(
                RoundedRectangle(cornerRadius: 20)
                    .foregroundStyle(appointment.hasRideScheduled ? Color.green : Color.red)
                    .opacity(0.5)
                    
            )
        }
    }
}

这是 CalendarView 的摘录,我将其称为 DayView:

LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 7), spacing: 20) {
                    ForEach($selectedDates) { $value in
                        
                        ZStack(alignment: .bottom) {
                            if value.day != -1 {
                                NavigationLink{
                                    if let index = dateHasAppointment(date: value.date) {
                                        DayView( appointments: value.appointments, currentDate: value.date)
                                    } else {
                                        DayView(currentDate: value.date)
                                    }
                                } label: {
                                    Text("\(value.day)")
                                        .background() {
                                            ZStack(alignment: .bottom) {
                                              
swift swiftui
1个回答
0
投票

我认为问题可能出在滚动视图上,尝试将其放在外面

© www.soinside.com 2019 - 2024. All rights reserved.