【发布时间】: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