【问题标题】:Collection View Controller crashing and not returning images集合视图控制器崩溃并且不返回图像
【发布时间】:2015-02-11 01:31:56
【问题描述】:

我正在构建一个集合视图以从 CloudKit 数据库返回图像。结果正在缓慢返回,但它们正在返回。但是,我遇到了经典的“致命错误:在展开可选值时意外发现 nil”错误,我无法弄清楚为什么或必须解决什么问题才能解决该错误。有人对我如何解决此问题有任何建议吗?

我的代码如下:

class CollectionViewController: UICollectionViewController  {

@IBOutlet var myCollectionView: UICollectionView!

var image = UIImage()
var matchedSelfie =  [UIImage]()
let reuseIdentifier = "MyCell"

override func viewDidLoad() {
    super.viewDidLoad()
   // self.canDisplayBannerAds = true


    println("Loading")

    var countedImages = matchedSelfie
    let publicDB = CKContainer.defaultContainer().publicCloudDatabase
    print(publicDB)

    let data = CKRecord(recordType: "theUsers")
    let pred = NSPredicate(format: "TRUEPREDICATE")
    let myQuery = CKQuery(recordType: "theUsers", predicate: pred)


    publicDB.performQuery(myQuery, inZoneWithID: nil) {
        results, error in
        println("Results from publicDB Query: \(results)")

        if error != nil {
            println(error)

        } else {
            for record in results {
                if var aselfie = record.objectForKey("selfie") as CKAsset! {
                    if var url = aselfie.fileURL {
                        var imageData = UIImage(contentsOfFile: url.path!)!
                       self.matchedSelfie.append(imageData)
                    }

                    println(self.matchedSelfie)
                    println("total selfies: \(self.matchedSelfie.count)")
                    var cellscount = self.matchedSelfie.count
                    }
                }
            }


    println("Done Loading")
     self.myCollectionView.reloadData()
        }
    }//View Did Load

 func collectionView(collectionView: UICollectionView, numberOfSectionsInCollectionView: Int) -> Int {
    self.myCollectionView.reloadData()
    return 1
}


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection: Int) -> Int {
        self.myCollectionView.reloadData()
    println("Selfies: \(self.matchedSelfie.count)")
    println(matchedSelfie.count)
     myCollectionView.reloadData()
    return self.matchedSelfie.count

    }



     override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as CollectionViewCell
        println("just seeing when this prints")
        cell.amatchedSelfie.image = image
        return cell
    }

}

【问题讨论】:

    标签: swift uicollectionview cloudkit


    【解决方案1】:

    你知道哪一行失败了吗?在将其分配给 cell.amatchedSelfie.image 时图像的值是多少?可能您可以在分配给 cell.amatchedSelfie.image 之前检查 nil

    【讨论】:

    • 在线崩溃:'cell.amatchedSelfie.image = image' 我不知道如何检查nil。
    • 只有在不为 nil 时才将图像分配给 cell.amatchedSelfie.image
    • if cell.amatchedSelfie != nil { cell.amatchedSelfie.image = image return cell } else { println("error") return cell }
    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 2011-11-21
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多