Swift UI 在没有导航控制器的情况下删除额外的列表空单元格 [英] Swift UI Remove Extra List Empty Cells without Navigation Controller
问题描述
Swift 用户界面 - 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
取自 this my answer
这篇关于Swift UI 在没有导航控制器的情况下删除额外的列表空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!