我想以与 Swift 对应的方式相同的方式运行它。在 Swift 版本中,我将 json 加载到解析器中,该解析器在 id 中加载列表(从最高到最低)并显示缩略图和标题(由 json 提供),单击时会显示附加 ID 的视频。
我目前没有看到与 SwiftUI(特别是列表视图)设置类似的东西,如果您能提供任何资源或示例,我将不胜感激。
这是 SwiftUI 文件之一
import SwiftUI
import Combine
struct VideoList: View {
@Environment(\.presentationMode) private var presentationMode
@ObservedObject private(set) var viewModel: ViewModel
@State private var isRefreshing = false
var btnBack : some View { Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image("Home") // set image here
.aspectRatio(contentMode: .fit)
.foregroundColor(.white)
}
}
}
var body: some View {
NavigationView {
List(viewModel.videos.sorted { $0.id > $1.id}, id: \.id) { video in
NavigationLink(
destination: VideoDetails(viewModel: VideoDetails.ViewModel(video: video))) {
VideoRow(video: video)
}
}
.onPullToRefresh(isRefreshing: $isRefreshing, perform: {
self.viewModel.fetchVideos()
})
.onReceive(viewModel.$videos, perform: { _ in
self.isRefreshing = false
})
}
.onAppear(perform: viewModel.fetchVideos)
.navigationViewStyle(StackNavigationViewStyle())
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: btnBack)
}
}
这是它正在加载的 json 的剪辑
{
"videos": [
{
"id": 97,
"name": "name",
"thumbnail": "https://videodelivery.net//thumbnails/thumbnail.jpg",
"description": "October 11th, 2020",
"video_link": "https://videodelivery.net//manifest/video.m3u8"
},
{
"id": 96,
"name": "name",
"thumbnail": "https://videodelivery.net//thumbnails/thumbnail.jpg",
"description": "October 11th, 2020",
"video_link": "https://videodelivery.net//manifest/video.m3u8"
},