gpt4 book ai didi

Swift: fatal error :在 dispatch_async dispatch_get_global_queue 中展开可选值时意外发现 nil

转载 作者:行者123 更新时间:2023-11-28 16:12:38 25 4
gpt4 key购买 nike

我有一个表格 View ,其中我显示了 3 个单元格,并且取决于像分页一样工作的 Collection View 单元格。所以当我想从 dispatch_async(dispatch_get_global_queue...) 获取表格 View 单元格上的图像时,我得到了

fatal error: unexpectedly found nil while unwrapping an Optional value

我的代码是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.videoTableView .dequeueReusableCellWithIdentifier("customTableViewCell") as! CustomTableViewCell

let videoDataObj:VideoData = subVideoArray .objectAtIndex(indexPath.row) as! VideoData
cell.titleLabel.text = videoDataObj.videoTitle as String
cell.descLabel.text = videoDataObj.videoDesc as String
cell.playButton.tag = indexPath.row + (pageIndex * noOfElement)

let imageUrl = NSURL(string: videoDataObj.videoImage as String)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){
let data = NSData(contentsOfURL: imageUrl!)
dispatch_async(dispatch_get_main_queue(), {

cell.videoImageView?.contentMode = .ScaleToFill
cell.videoImageView?.image = UIImage(data: data!)

cell.setNeedsLayout()
cell.layoutIfNeeded()
})
}

return cell

}

View 的屏幕截图是:enter image description here提前致谢

最佳答案

是的!!!我检查了我的代码并得到了解决方案。我犯了一个愚蠢的错误。尝试一下..更新代码。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){

if let data = NSData(contentsOfURL: imageUrl!){
dispatch_async(dispatch_get_main_queue(), {

cell.videoImageView?.contentMode = .ScaleToFill
cell.videoImageView?.image = UIImage(data: data)

cell.setNeedsLayout()
cell.layoutIfNeeded()
})

}

关于Swift: fatal error :在 dispatch_async dispatch_get_global_queue 中展开可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39391687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com