ios - SwiftUI列表未显示所有项目

标签 ios swift iphone xcode swiftui

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

事件画面

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)
            }


    }
}

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)
                    }
                }
            }
        }
    }
}

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)
    }
}

EventItemView
struct EventItemView: View {
    var event: Event
    var body: some View {
        VStack {
            Color.red
                .frame(width: 100, height: 100, alignment: .center)
                .cornerRadius(5)
            Text(event.title)
            .bold()
                .frame(width: 100, alignment: .leading)
                .foregroundColor(.white)
                .font(.system(size: 10))
            Text(event.date)
                .frame(width: 100, alignment: .leading)
                .foregroundColor(.white)
                .font(.system(size: 10))
        }
        .padding(.trailing, 8)
    }
}

Screenshot

最佳答案

它需要使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
        }
    }
}

关于ios - SwiftUI列表未显示所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59850967/

相关文章:

ios - 使用 Cocoa API 时,所有代码都在运行循环中运行吗?

ios - 获取 Youtube 视频 viewCount Youtube API v3/Swift

module - xcrun swift 在命令行上生成 <unknown> :0: error: could not load shared library

ios - 发送带有 iPhone 应用程序链接的短信或推送通知

ios - 更改 UISearchController 在 Swift 中的位置

iphone - 有没有iOS runloop机制的指南?

iPhone notification_post 通知

iphone - 对象 : Make a NSURL with a String that does not conform to RFC 2396

ios - layoutSubviews 在 UITableViewCell 上调用了两次

ios - 触摸时更改 UICollectionViewCell 的颜色