SwiftUI列表未显示所有项目

时间:2020-01-22 00:10:00

标签: ios swift iphone xcode swiftui

我正在尝试显示iOS中横向滚动列表的列表。有些列表显示,但其他列表则没有,即使它们都有数据。我放置了一个调试点,发现每个部分都调用了带有ForEach的{​​{1}}。我不确定自己在做什么错。

EventScreen

EventItemView

EventSectionView

struct EventScreen: View {

    @State
    var currentPage: Int = 0

    var viewControllers =
        IncentiveSource().getFeaturedIncentives().map({ incentive in
            UIHostingController(rootView: EventFeatureView(event: incentive.toEvent()))
        })

    var response: [EventSection] = IncentiveSource().getIncentives().sections.toEventSections()


    var body: some View {
            NavigationView {
                List {
                    EventViewController(controllers: self.viewControllers, currentPage: self.$currentPage)
                        .listRowInsets(EdgeInsets())
                        .frame(height: 600)
                    ForEach(self.response) { section in
                        EventSectionView(eventSection: section)
                    }
                }
                .navigationBarTitle(Text("Events").foregroundColor(Color.black), displayMode: .inline)
            }


    }
}

EventItemView

struct EventSectionView: View {

    var eventSection: EventSection

    var body: some View {
        VStack(alignment: .leading) {
            SectionTextView(text: eventSection.category.typeName())
                .frame(alignment: .leading)
            ScrollView(.horizontal, showsIndicators: true) {
                HStack(alignment: .top, spacing: 0) {
                    ForEach(self.eventSection.events) { event in
                        EventItemView(event: event)
                    }
                }
            }
        }
    }
}

struct SectionTextView: View {
    var text: String
    var body: some View {
        return Text(text)
        .bold()
            .font(.system(size: 18, weight: .heavy))
            .foregroundColor(Color(ColorTheme.brandBlue.value))
            .padding(.bottom, 4)
    }
}

Screenshot

1 个答案:

答案 0 :(得分:1)

它需要使EventSectionView中的每个水平滚动条像下面一样唯一

struct EventSectionView: View {

    var eventSection: EventSection

    var body: some View {
        VStack(alignment: .leading) {
            SectionTextView(text: eventSection.category.typeName())
                .frame(alignment: .leading)
            ScrollView(.horizontal, showsIndicators: true) {
                HStack(alignment: .top, spacing: 0) {
                    ForEach(self.eventSection.events) { event in
                        EventItemView(event: event)
                    }
                }
            }.id(UUID().uuidString() // << unique
        }
    }
}