![](/img/trans.png)
[英]How do I return from a child view to a parent view by tapping a row element in a list in SwiftUI?
[英]How do I hide the list row separator of a SwiftUI List view for only the first and last items?
可以編寫多個內聯三元語句,但有沒有更簡潔的方法?
具有以下功能:
extension View {
@ViewBuilder
fileprivate func hideListRowSeparatorIfFirstOrLastItem<Data: RandomAccessCollection<ID>, ID: Identifiable>(data: Data, item: ID) -> some View {
if data.first?.id == item.id {
listRowSeparator(.hidden, edges: .top)
} else if data.last?.id == item.id {
listRowSeparator(.hidden, edges: .bottom)
} else {
self
}
}
}
您可以巧妙地在線添加此屬性:
struct MyView: View {
let model = Model()
var body: some View {
List {
Section("Section") {
ForEach(model.items) { item in
Text("Hello \(item.value)")
.hideListRowSeparatorIfFirstOrLastItem(data: model.items, item: item)
}
}
}
.listStyle(.plain)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.