无法使用Collection View从Parse接收图像

时间:2015-07-24 07:06:38

标签: ios swift parse-platform uicollectionview

我正在尝试从解析中检索图像并将其显示到集合视图中。 Xcode告诉我,我不能用参数类型字符串调用dequeueReusableCellWithReuseIdentifier 我也错误地告诉我SingleRowCell无法转换为UICollectionView。我的Collection View实现不正确吗?我能够使用表格视图执行此操作。谢谢。

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let singleCell:SingleRowCell = collectionView.dequeueReusableCellWithReuseIdentifier("mySingleCell")as! SingleRowCell
        singleCell.radLabel.text = imageText [indexPath.row]
        imageFiles[indexPath.row].getDataInBackgroundWithBlock{
            (imageData: NSData?, error: NSError?) -> Void in
            if imageData != nil {
                let image = UIImage(data: imageData!)
                singleCell.radImageView.image = image
            }else {
                println(error)
            }}

        return mysingleCell
    }
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20;
    }

}

更新

override func viewDidLoad() {
    super.viewDidLoad()

    self.collectionView!.registerClass(SingleRowCell.self, forCellWithReuseIdentifier: "mySingleCell")


    // Do any additional setup after loading the view.
    var query = PFQuery(className: "rad")
    query.whereKey("uploader", equalTo: PFUser.currentUser()!)
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock{
        (posts: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            for post in posts!{
                self.imageFiles.append(post["imageFile"]as! PFFile)
                self.imageText.append(post["imageText"]as! String)
            }


            println(self.imageFiles.count)
            self.topsCollectionView.reloadData()

        } else {
            println(error)
        }}}

1 个答案:

答案 0 :(得分:1)

首先,为CollectionView的单元格注册一个类:

// Using MyClass
Type objType = Type.GetType("VsLogAnalyzer." + "MyClass");
MyInterface myObj = Activator.CreateInstance(objType) as MyInterface;
myObj.showData();
// Using MyClass2
objType = Type.GetType("VsLogAnalyzer." + "MyClass2");
myObj = Activator.CreateInstance(objType) as MyInterface;
myObj.showData();

然后,在self.collectionView!.registerClass(SingleRowCell.self, forCellWithReuseIdentifier: "mySingleCell") 方法中,

cellForItemAtIndexPath