ios - Swift UI 删除多余的列表空单元格,无需导航 Controller

标签 ios swift xcode swiftui

Swift UI - Xcode

问题:当我的列表初始化时,我有多余的单元格。然而,与其他帖子不同的是,我没有导航 View 。为了从我的列表中删除多余的单元格,是否必须有一个 NavigationView?

我尝试实现导航 View ,但我不确定它是否是强制性的以及如何正确实现。

.navigationBarTitle("List")
.listStyle(GroupedListStyle())

我想在最后一个“当前”单元格正下方启动一个新的文本或 UIButton,而不需要额外的空格。

enter image description here

var body: some View {


    VStack(alignment: .leading) {


        Image("covidImage")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .cornerRadius(5)

        Text("COVID DATA")
            .font(.system(.title, design: .rounded))
            .bold()
            .lineLimit(3)
            .padding(.bottom, 3)
            .padding(.leading, 10)

        Text("Represented by Sate in the United States")
            .font(.subheadline)
            .foregroundColor(.secondary)
            .padding(.leading, 10)
            .padding(.bottom, 0)

        Text("Current State: CA")
        .font(.subheadline)
        .foregroundColor(.secondary)
        .padding(.leading, 10)
        .padding(.bottom, 10)

      //List to Initialize TableView Data
        List(covid) { covidData in
            Image(systemName: "photo")
                .padding()
            HStack {
                Text(covidData.dataTitle).frame(maxWidth: .infinity, alignment: .leading)

                Text(covidData.dataValue)
                    .font(.subheadline)
                .frame(maxWidth: .infinity, alignment: .trailing)
                    //.color(.gray)


            }


        }//END List


    }//END Original VStack



}//END Some View

最佳答案

它们不是空单元格。 List 只是填充所有可用空间并在没有内容的地方绘制空行占位符。在您的场景中,需要将 List 的高度限制为其内容。

这里有一个解决方案。使用 Xcode 11.4/iOS 13.4 进行测试

为了获得更好的可见性,让我们将您的列表分离到名为 CovidDataList 的自己的 View 中,然后在您提供的 body 中:

  //List to Initialize TableView Data
    CovidDataList(covid: self.covid)

    }//END List

注意1:列表的高度在运行时更新,因此应该在运行的应用程序中进行测试(而不是在预览中)

注2:我使用复制的封面数据模型进行了测试,因此您的代码可能需要进行一些调整

struct CovidDataList: View {
    @Environment(\.defaultMinListRowHeight) var minRowHeight

    let covid: [CovidData]
    @State private var listHeight = CGFloat.infinity

    var body: some View {
        List {
            ForEach(covid, id: \.self) { covidData in
                HStack {
                    Image(systemName: "photo")
                        .padding()
                    HStack {
                        Text("Title").frame(maxWidth: .infinity, alignment: .leading)

                        Text("Value")
                            .font(.subheadline)
                            .frame(maxWidth: .infinity, alignment: .trailing)
                    }
                }
                .listRowInsets(EdgeInsets()).padding(.horizontal)
            }
            .anchorPreference(key: BoundsPreferenceKey.self, value: .bounds) { [$0] }
        }
        .backgroundPreferenceValue(BoundsPreferenceKey.self) { rects in
            GeometryReader { gp in
                self.updateListFrame(gp: gp, anchors: rects)
            }
        }.frame(maxHeight: listHeight)
    }

    private func updateListFrame(gp: GeometryProxy, anchors: [Anchor<CGRect>]) -> some View {
        let contentHeight = anchors.reduce(CGFloat.zero) { $0 + gp[$1].size.height }
        DispatchQueue.main.async {   // << required !!
            self.listHeight = max(contentHeight, self.minRowHeight * CGFloat(self.covid.count))
        }
        return Color.clear
    }
}

注意:BoundsPreferenceKey 取自 this my answer

关于ios - Swift UI 删除多余的列表空单元格,无需导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62054456/

相关文章:

objective-c - 如何在iOS模拟器中显示状态栏?

Swift:如何向 init 方法添加功能

ios - 单例中的委托(delegate)模式不起作用

ios - Unwind Segue 在 iOS 8 中不起作用

ios - 录音时裁剪音频文件的方法?

ios - 如何检测月/年变化

ios - #UIBarButtonItem 上的选择器

swift - 用户不允许定位时如何处理

ios - 以编程方式从 inputAccessoryView 执行 textFieldShouldReturn

ios - 来自 iTunes Connect 的有关应用程序状态的令人困惑的消息?