无法从Cloud Firestore加载图片至TableView行的问题求助
TableView单元格图片无法加载问题
我实现了ListOfVideoLessonsTableViewController,预期每行TableView单元格会显示图片,但实际在模拟器运行应用时,仅显示标题和副标题,图片完全无法加载。
相关截图说明:
- 第一张截图:展示了设计预期的TableView样式,每行应包含图片、标题与副标题
- 第二张截图:模拟器实际运行界面,仅显示标题和副标题,无图片;同时包含控制台打印信息及Firestore数据库结构
1. ListOfVideoLessonsTableViewController代码
import UIKit import AVKit import AVFoundation import FirebaseFirestore import Combine class ListOfVideoLessonsTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let pinchGesture = UIPinchGestureRecognizer() private var viewModel = VideosViewModel() private var cancellable: AnyCancellable? var player = AVPlayer() var playerViewController = AVPlayerViewController() @IBOutlet var table: UITableView! override func viewDidLoad() { super.viewDidLoad() self.viewModel.fetchData() self.title = "Video Lessons" table.delegate = self table.dataSource = self cancellable = viewModel.$videos.sink { _ in DispatchQueue.main.async{ self.table.reloadData() } } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print("videos count = ", viewModel.videos.count) return viewModel.videos.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let video = viewModel.videos[indexPath.row] tableView.tableFooterView = UIView() cell.textLabel?.text = video.name cell.detailTextLabel?.text = video.lessonName cell.accessoryType = .disclosureIndicator let image = UIImage(named: video.imageName) cell.imageView?.image = image let backgroundView = UIView() backgroundView.backgroundColor = UIColor(named: "VideoLessonsCellHighlighted") cell.selectedBackgroundView = backgroundView cell.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14) cell.detailTextLabel?.font = UIFont(name: "Helvetica", size: 12) return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) playVideo(at: indexPath) } func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.contentView.backgroundColor = UIColor(named: "VideoLessonsCellHighlighted") cell.textLabel?.highlightedTextColor = UIColor(named: "textHighlighted") cell.detailTextLabel?.highlightedTextColor = UIColor(named: "textHighlighted") } } func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.contentView.backgroundColor = nil } } func playVideo(at indexPath: IndexPath){ let selectedVideo = viewModel.videos[indexPath.row] let videoURL = URL(string: selectedVideo.fileURL) player = AVPlayer(url: videoURL!) playerViewController.player = player self.present(playerViewController, animated: true, completion: { self.playerViewController.player?.play() }) } }
2. VideosViewModel代码
import Foundation import FirebaseFirestore class VideosViewModel: ObservableObject { @Published var videos = [Video]() private var db = Firestore.firestore() func fetchData() { db.collection("videos").addSnapshotListener { [self] (querySnapshot, error) in guard let documents = querySnapshot?.documents else { print("No Documents") return } self.videos = documents.map { (queryDocumentSnapshot) -> Video in let data = queryDocumentSnapshot.data() let name = data["name"] as? String ?? "" let imageName = data["imageName"] as? String ?? "" let lessonName = data["lessonName"] as? String ?? "" let fileURL = data["fileURL"] as? String ?? "" print(data) return Video(name: name , imageName: imageName , lessonName: lessonName, fileURL: fileURL ) } } } }
3. Video结构体定义
import Foundation struct Video { var name: String var imageName: String var lessonName: String var fileURL: String }
内容的提问来源于stack exchange,提问作者Vugar
相关产品推荐
相关产品推荐

