[英]Multiple Parse.com row images displaying in Collectionview?
我只有一個班級,叫做“館藏”。 在該類中,我有多個列用於“名稱”,“位置”,“ image1”,“ image2”,“ image3”和“ image4”。
如果我只處理單個圖像和一個表格視圖,那么我就不會有麻煩。 但是我正在冒險探索我現在所了解的東西,而且似乎無法在任何地方找到任何與Swift相關的示例。 我看到它提到要么使用少量的列文件/圖像就可以了(就像我正在做的3-4步一樣),如果要使用更多的列來做關系(不管是什么)。
不幸的是,我還沒有找到任何有關如何實際下拉圖像(如果存在)的示例。
代碼在下面,但是它所做的就是提取image1圖像,因為我也不知道如何在集合視圖中獲取image2,image3和image4。
...在這里刪除了一堆無效的代碼...
在此處添加新代碼:
這是變量:
class ParseCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate {
var currentObject : PFObject?
var collectionImages: [PFFile]?
var tappedObjectID: String!
var imageObject0: PFFile!
var imageObject1: PFFile!
var imageObject2: PFFile!
var imageObject3: PFFile!
var imageObject4: PFFile!
var imageObject5: PFFile!
var imageArray: [UIImage] = []
var collectionImage0: UIImage!
var collectionImage1: UIImage!
var collectionImage2: UIImage!
var collectionImage3: UIImage!
var collectionImage4: UIImage!
var collectionImage5: UIImage!
var imageView: PFImageView!
這是非常丑陋的代碼...我確定這對數組來說是完美的工作,但是我不確定如何做到這一點:
func loadCollectionViewData() {
// Build a parse query object
var query = PFQuery(className:"Collections")
query.whereKey("objectId", equalTo:tappedObjectID)
query.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) records.")
self.imageArray.removeAll(keepCapacity: true)
for object in objects! {
self.imageObject0 = object["image0"] as! PFFile
self.imageObject0.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
self.collectionImage0 = UIImage(data:imageData!)!
println("Image0 successfully retrieved")
println(self.collectionImage0)
}
self.imageArray.append(self.collectionImage0)
self.collectionView.reloadData()
println("image0 imageArray: \(self.imageArray)")
})
self.imageObject1 = object["image1"] as! PFFile
self.imageObject1.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
self.collectionImage1 = UIImage(data:imageData!)!
println("Image1 successfully retrieved")
println(self.collectionImage1)
}
self.imageArray.append(self.collectionImage1)
self.collectionView.reloadData()
println("image1 imageArray: \(self.imageArray)")
})
self.imageObject2 = object["image2"] as! PFFile
self.imageObject2.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
self.collectionImage2 = UIImage(data:imageData!)!
println("Image2 successfully retrieved")
println(self.collectionImage2)
}
self.imageArray.append(self.collectionImage2)
self.collectionView.reloadData()
println("image0 imageArray: \(self.imageArray)")
})
self.imageObject3 = object["image3"] as! PFFile
self.imageObject3.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
self.collectionImage3 = UIImage(data:imageData!)!
println("Image3 successfully retrieved")
println(self.collectionImage3)
}
self.imageArray.append(self.collectionImage3)
self.collectionView.reloadData()
println("image3 imageArray: \(self.imageArray)")
})
self.imageObject4 = object["image4"] as! PFFile
self.imageObject4.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
self.collectionImage4 = UIImage(data:imageData!)!
println("Image4 successfully retrieved")
println(self.collectionImage4)
}
self.imageArray.append(self.collectionImage4)
self.collectionView.reloadData()
println("image4 imageArray: \(self.imageArray)")
})
self.imageObject5 = object["image0"] as! PFFile
self.imageObject5.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
self.collectionImage5 = UIImage(data:imageData!)!
println("Image0 successfully retrieved")
println(self.collectionImage5)
}
self.imageArray.append(self.collectionImage5)
self.collectionView.reloadData()
println("image5 imageArray: \(self.imageArray)")
})
}
} else {
NSLog("Error: %@ %@", error!, error!.userInfo!)
}
})
self.collectionView.reloadData()
}
這是細胞的東西:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ParseCollectionViewCell
cell.cellImage.image = UIImage(named: "question")
cell.cellImage.image = imageArray[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let tappedObject = imageArray[indexPath.row]
performSegueWithIdentifier("CollectionViewToDetailView", sender: tappedObject)
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailObject : PFObject?
if let imageArray = sender as? PFObject{
detailObject = sender as? PFObject
} else {
// No cell selected in collectionView
detailObject = PFObject(className:"Collections")
}
// Get a handle on the next story board controller and set the currentObject ready for the viewDidLoad method
var detailScene = segue.destinationViewController as! ParseDetailViewController
detailScene.detailObject = (detailObject)
detailScene.currentObject = (currentObject)
}
由於隨機加載,我要么需要弄清楚如何以一定順序強制加載圖像,要么必須弄清楚在點擊它時哪個圖像才可以正確地傳遞給下一個ViewController。是局部視圖控制器。
您必須為單元格中的圖像制作更多的UIImageViews(或PFImageViews),然后為其余圖像重復第一張圖像的圖像(但更改其名稱以匹配它們在解析時存儲的列)
另外,您可能想要實現PFImageViews並使用其文件屬性和loadInBackground()方法。
編輯:您可以遍歷接收到的對象並將圖像放置到新數組中,並將其用於收藏視圖。
代碼示例:
var parseOjbect = ["image1":NSData(), "image2":NSData(),"image3":NSData(),"image4":NSData()]
var receivedParseObjects = [parseOjbect]
var photosForCollectionView = [NSData]()
for receivedParseObject in receivedParseObjects {
photosForCollectionView.append(receivedParseObject["image1"]!)
photosForCollectionView.append(receivedParseObject["image2"]!)
photosForCollectionView.append(receivedParseObject["image3"]!)
photosForCollectionView.append(receivedParseObject["image4"]!)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.