繁体   English   中英

在Collectionview中显示多个Parse.com行图像?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM