Swift UI删除没有导航控制器的多余列表空白单元格 [英] Swift UI Remove Extra List Empty Cells without Navigation Controller
问题描述
快速用户界面-Xcode
问题:初始化列表时,我还有多余的单元格.但是,与其他帖子不同,我没有导航视图.为了从我的列表中删除多余的单元格,是否必须具有NavigationView.
Problem: I have extra cells when my list is initialized. However, unlike other posts I do not have a navigation view. Is it mandatory to have a NavigationView in order to remove the extra cells from my list.
我尝试实现导航视图,但不确定是否必须使用导航视图以及如何正确实现.
I have tried to implement a Navigation View but im not sure if its mandatory and how to properly implement.
.navigationBarTitle("List")
.listStyle(GroupedListStyle())
我想在最后一个当前"单元格的正下方开始一个新的Text或UIButton,而没有多余的空格.
I would like to start a new Text or UIButton directly below the last "Current" cell without extra white spaces.
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
的高度限制为其内容.
They are not empty cells. The List
just fills all available space and draws empty rows placeholders where there is no content. In your scenario is needed to limit height of List
to its content.
这是一个解决方案.使用Xcode 11.4/iOS 13.4进行了测试
Here is a solution. Tested with Xcode 11.4 / iOS 13.4
为了获得更好的可见性,我们将您的列表分为名为 CovidDataList
的自己的视图,然后在您提供的 body
中:
For better visibility let's separate your list into own view named CovidDataList
, then here is in your provided 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
来自这是我的答案
这篇关于Swift UI删除没有导航控制器的多余列表空白单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!