ios - SwiftUI - 如何创建带有标题和详细信息标签的列表?
问题描述
我正在尝试重新创建一个带有标题标签和详细标签的列表视图,就像许多标准 iOS 应用程序一样:
https://developer.apple.com/documentation/uikit/uitableviewcell/1623273-detailtextlabel
我想要左边的标题标签,右边的详细标签?有没有在 SwiftUI 中构建 UI 的标准实现?
谢谢!
解决方案
为了代码简洁,我将这个小任务的不同部分分开。
每个单元格数据的模型:
struct CellModel {
let title: String
let detail: String
}
表示每个单独单元格的视图:
struct DetailCellView: View {
let model: CellModel
var body: some View {
HStack() {
Text(model.title)
Spacer()
Text(model.detail)
}
}
}
和列表(表)视图:
struct ContentView : View {
// Data Source
let cells = [
CellModel(title: "Text 1", detail: "Detail 1"),
CellModel(title: "Text 2", detail: "Detail 2"),
CellModel(title: "Text 3", detail: "Detail 3")
]
var body: some View {
List(cells.identified(by: \.title)) { model in
DetailCellView(model: model)
}
}
}
- 默认细节样式
如果您想要一个简单的默认详细信息构建器,请执行以下操作:
struct DetailCellView: View {
@State var image: UIImage = UIImage()
@State var title: String = ""
@State var detail: String = ""
var body: some View {
HStack() {
Image(uiImage: image)
Text(title)
Spacer()
Text(detail)
}
}
}
推荐阅读
- javascript - Google 跟踪代码管理器如何知道浏览量已发生?
- c - 分配字符串数组
- python - 如何从这些彩色图像中提取文本?
- c# - CORS 问题随机发生
- reactjs - 我无法设置哪些元素将显示在 js 分页反应的页面上
- reactjs - 访问现有的 html
- java - 带有 Jersey 的 Spring Boot - 过滤请求正文属性
- math - 找到一个公式来知道一棵树有多少“孩子”,如果它是一棵树子树
- kiwi-tcms - 如何更改测试用例上的 kiwi 模板文本?
- ruby-on-rails - 如何在rails before_create钩子中编写if else条件,以便仅在条件为真时保存对象?