使用Alamofire调用数据并在Viewcontroller上传递自定义单元格.错误:解开可选值包装时意外发现 nil



崩溃的图像 当我通过阿拉莫火检索数据时,我遇到了问题。在这里,我定义了一个函数,其中数据在完成时传递到视图控制器。

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)
}
}
}

使用以下代码,我在视图控制器中获取数据。

videoModel.getVideoDetail(video_id: video_Id) { (Details) in
self.videoDetails = Details
self.playVideo()
}

这是我的表格视图: 此单元格是自定义单元格。我已经在我的视图上注册了所有自定义单元(UINIBS(DidLoad

// 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)
}

像这样替换;

if let data = jsonData {
let root = try? JSONDecoder().decode(DetailsData.self, from: data)
getDetail = root.data
}

如果你的 jsondata 为 nil,它会让你的应用程序崩溃,不会被 catch 控制。

顺便说一句,你应该停止强制变量。那不安全。使用 if 或 guard let 以防止崩溃。

there is nil  `incategory_name` or  `category_name`

像这样使用

cell.likeLabel.text = videoDetails!.category_name ?? "" 
cell.dislikeLabel.text = videoDetails!.category_name ?? "" 

我已经解决了这个问题。

我将详细信息直接加载到CustomCell文件中,然后使用didSet加载UI。代码如下所示。

详细信息单元格:

class DetailsCell: UITableViewCell {
// Temp Helper for Like Video
var like = false
var unlike = false
var likeDislike = LikeDislikeHelper()
var videoModel = VideoModel()
var videoDetails: VideoDetail! {
didSet {
updateUI()
}
}
weak var delegate: DetailsCellDelegate?
@IBOutlet weak var likeButton: AnimatableButton!
@IBOutlet weak var dislikeButton: AnimatableButton!
@IBOutlet weak var shareButton: AnimatableButton!
@IBOutlet weak var playlistButton: AnimatableButton!
@IBOutlet weak var likeLabel: UILabel!
@IBOutlet weak var dislikeLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func getDetails(video_id: String) {
videoModel.getVideoDetail(video_id: video_id) { (Details) in
self.videoDetails = Details
}
}
func updateUI() {
self.likeLabel.text = "(videoDetails.likes)"
self.dislikeLabel.text = "(videoDetails.dislikes)"
}

视图控制器:

let cell = tableView.dequeueReusableCell(withIdentifier: "DetailsCell") as! DetailsCell
cell.checkIsLiked(commentVideoId: commentVideoId)
cell.getDetails(video_id: video_Id)
cell.delegate = self
return cell

感谢所有帮助过的人

相关内容

最新更新