gpt4 book ai didi

ios - Swift UI 删除没有导航 Controller 的额外列表空单元格

转载 作者:行者123 更新时间:2023-12-01 21:47:33 34 4
gpt4 key购买 nike

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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com