崩溃的图像 当我通过 Alamofire 检索数据时遇到问题。在这里,我定义了一个函数,其中数据通过完成传递给视图控制器。
func getVideoDetail(video_id: String, completionHandler: @escaping (VideoDetail!) -> Void) {
let PageURL = URL(string: "\(websiteLinks.api)/?type=get_detailss&video_id=\(video_id)")
Alamofire.request(PageURL!).responseJSON { (response) in
switch response.result {
case .success:
var getDetail: VideoDetail?
let jsonData = response.data
do {
let root = try JSONDecoder().decode(DetailsData.self, from: jsonData!)
getDetail = root.data
//THIS IS WHERE YOUR PREVIOUS ERROR OCCURRED
} catch {
print("Error: \(error)")
}
DispatchQueue.main.async {
completionHandler(getDetail!)
}
case .failure(let error):
print(error)
}
}
}
使用以下代码,我在 ViewController 中获取数据。
videoModel.getVideoDetail(video_id: video_Id) { (Details) in
self.videoDetails = Details
self.playVideo()
}
这是我的 TableView:这个 Cell 是一个 Customcell。我已经在我的 viewDidLoad 上注册了所有 Customcell (UINIBS)
// REGISTER NIBS FOR THE CELLS
tableView.register(UINib(nibName: "DetailsCell", bundle: nil), forCellReuseIdentifier: "DetailsCell")
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "DetailsCell") as! DetailsCell
cell.likeLabel.text = videoDetails!.category_name!
cell.dislikeLabel.text = videoDetails!.category_name!
cell.delegate = self
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerUserCell") as! PlayerUserCell
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "newestCell") as! NewestCell
return cell
}
}
但是如果我现在启动应用程序并选择一个单元格,程序就会崩溃。我在这里做错了什么?
这是我的 HomeController 上的 didSelectRow 代码。我在这里传递了 getDetails 函数的 video_id:
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let playerVC = storyboard.instantiateViewController(withIdentifier: "playervc") as! PlayerVC
playerVC.videoURL = URL(string: featuredVideos[indexPath.row - 2].video_location!)
playerVC.video_Id = featuredVideos[indexPath.row - 2].video_id
playerVC.commentVideoId = featuredVideos[indexPath.row - 2].id
self.present(playerVC, animated: true, completion: nil)
}