微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 UITableView 中滚动得非常快时,我得到一个零错误不知道如何修复

如何解决在 UITableView 中滚动得非常快时,我得到一个零错误不知道如何修复

我包含了下面的所有代码

不过……

该单元格有两个图像,一个背景图像(已模糊)和另一个位于未模糊图像之上的图像。当用户在 tableview 中快速滚动时,我收到以下错误

Unexpectedly found nil while unwrapping an Optional value: file csnios/CellEbayItems.swift,line 135

但是,我有代码可以检查它。它也在 do/catch 中,但我能做些什么来修复这个错误

if (self.imageBlur.image! != nil) {
    self.blurEffect()
}

class CellEbayItems: UITableViewCell {

    @IBOutlet var mainImage: UIImageView!
    @IBOutlet var imageBlurView: UIView!
    @IBOutlet var imageBlur: UIImageView!
    @IBOutlet var sellerUsername: UILabel!
    @IBOutlet var sellerStats: UILabel!
    
    @IBOutlet var itemTitle: UILabel!
    @IBOutlet var itemCurrentPrice: UILabel!
    @IBOutlet var itemShippingCost: UILabel!
    
    @IBOutlet var itemTimeLeft: UILabel!
    
    @IBOutlet var itemDetailsBackground: UIView!
    
    @IBOutlet var itemListingType: UILabel!
    @IBOutlet var itemNumberBidders: UILabel!
    @IBOutlet var itemNumberWatchers: UILabel!
    
    
    var context = CIContext(options: nil)
    
    
    func configureCell(item: EbayResultsModel) {
        sellerUsername.text = item._sellerUsername
        
        if (item._sellerFeedbackrating == "100.0") {
            sellerStats.text = "100% (\(item._sellerFeedbackscore!))"
        } else {
            sellerStats.text = "\(item._sellerFeedbackrating!)% (\(item._sellerFeedbackscore!))"
        }
        
        itemTitle.text = item._title
        itemCurrentPrice.text = "$\(item._currentPrice!)"
        
        if (item._shipping == "TBD") {
            itemShippingCost.text = "Shipping UnkNown"
        } else if (item._shipping == "free" || item._shipping == "0.00") {
            itemShippingCost.text = "Free Shipping"
        } else {
            itemShippingCost.text = "$\(item._shipping!) S/H"
        }
        
        itemNumberBidders.isHidden = true
        
        var listingTypestring = "";
        if (item._listingType == "FixedPrice") {
            listingTypestring = "Buy It Now";
        } else if (item._listingType == "StoreInventory") {
            listingTypestring = "Buy It Now";
        } else if (item._listingType == "AuctionWithBIN") {
            listingTypestring = "Auction w/ BIN";
        } else if (item._listingType == "Auction") {
            listingTypestring = "Auction";
            self.itemNumberBidders.isHidden = false
        }

        itemListingType.text = "\(listingTypestring)"

        if (item._bestOfferEnabled == "true") {
            itemListingType.text = "\(listingTypestring) w/ BO"
        }
        
        if (item._bidCount == "1") {
            itemNumberBidders.text = "\(item._bidCount!) Bidder"
        } else {
            itemNumberBidders.text = "\(item._bidCount!) Bidders"
        }

        if (item._watchCount == "1") {
            itemNumberWatchers.text = "\(item._watchCount!) Watcher"
        } else {
            itemNumberWatchers.text = "\(item._watchCount!) Watchers"
        }
        
        
        let split = item._timeLeft.components(separatedBy: CharacterSet(charactersIn: "PDTHMS"))

        let days = Int(split[1])!
        let hours = Int(split[3])!
        let minutes = Int(split[4])!
        let seconds = Int(split[5])!
        
       
        var formatDateTime =  "\(days)d \(hours)h \(minutes)m \(seconds)s left"

        if (days >= 1) {
            if (days == 1) {
                formatDateTime = "More than \(days)d remain."
            } else {
                formatDateTime = "More than \(days)d remain."
            }
        } else {
            if (days == 0) {
                if (hours == 1) {
                    formatDateTime = "More than \(hours)h remain."
                } else {
                    formatDateTime = "More than \(hours)h remain."
                }
                //formatDateTime = String.format("%sh %sm %ss left",hours,minutes,seconds);
            }
            if (hours == 0) {
                if (minutes > 2) {
                    formatDateTime = "More than \(minutes)m remain."
                } else {
                    formatDateTime = "ACT FAST!\nLess than 2m."
                }
                //formatDateTime = String.format("%sm %ss left",seconds);
            }
            if (minutes == 0) {
                formatDateTime = "\(seconds)s left"
            }
        }


        itemTimeLeft.text = formatDateTime
                
        
        do {
            imageBlur.sd_setimage(with: URL(string: item._galleryURL),placeholderImage: UIImage(named: ""))
            mainImage.sd_setimage(with: URL(string: item._galleryURL),completed: {(image,error,type,url) in
                self.imageBlur.image = image
                if (self.imageBlur.image! != nil) { //Unexpectedly found nil while unwrapping an Optional value: file csnios/CellEbayItems.swift,line 135 HERE
                    self.blurEffect()
                }
            })
        }
        catch {
            // LOG ERROR
        }
        
        itemDetailsBackground.layer.cornerRadius = 15
    }
    
    func blurEffect() {

        let currentFilter = CIFilter(name: "CIGaussianBlur")
        let beginImage = CIImage(image: self.imageBlur.image!)
        currentFilter!.setValue(beginImage,forKey: kCIInputimageKey)
        currentFilter!.setValue(10,forKey: kCIInpuTradiusKey)

        let cropFilter = CIFilter(name: "CICrop")
        cropFilter!.setValue(currentFilter!.outputimage,forKey: kCIInputimageKey)
        cropFilter!.setValue(CIVector(cgRect: beginImage!.extent),forKey: "inputRectangle")

        let output = cropFilter!.outputimage
        let cgimg = self.context.createCGImage(output!,from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        self.imageBlur.image = processedImage
    }
}

解决方法

这行没有意义

self.imageBlur.image! != nil

您正在强制解包 image,它会在 nil 时导致应用崩溃,然后您将其与 nil 进行比较。只需像这样检查:self.imageBlur.image != nil

一般来说,在 IBOutlets 之外强制展开不是一个好主意。

This question 有一个非常全面的答案,解释了您应该如何在 Swift 中处理可选项。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。