无法使用 Collection View 从 Parse 接收图像
Can't receive images from Parse using Collection View
我正在尝试从解析中检索图像并将其显示到集合视图中。 Xcode 让我无法使用参数类型字符串调用 dequeueReusableCellWithReuseIdentifier
。
我也有一个错误告诉我 SingleRowCell
不能转换为 UICollectionView
。我对 Collection View 的实现不正确吗?我能够使用 table 视图来做到这一点。谢谢你。
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)
}}}
首先,为 CollectionView 的单元格注册一个 class:
self.collectionView!.registerClass(SingleRowCell.self, forCellWithReuseIdentifier: "mySingleCell")
然后,在cellForItemAtIndexPath
方法中,
let singleCell: SingleRowCell = collectionView.dequeueReusableCellWithReuseIdentifier("mySingleCell", forIndexPath: indexPath) as! SingleRowCell
// config the cell
return singleCell
我正在尝试从解析中检索图像并将其显示到集合视图中。 Xcode 让我无法使用参数类型字符串调用 dequeueReusableCellWithReuseIdentifier
。
我也有一个错误告诉我 SingleRowCell
不能转换为 UICollectionView
。我对 Collection View 的实现不正确吗?我能够使用 table 视图来做到这一点。谢谢你。
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)
}}}
首先,为 CollectionView 的单元格注册一个 class:
self.collectionView!.registerClass(SingleRowCell.self, forCellWithReuseIdentifier: "mySingleCell")
然后,在cellForItemAtIndexPath
方法中,
let singleCell: SingleRowCell = collectionView.dequeueReusableCellWithReuseIdentifier("mySingleCell", forIndexPath: indexPath) as! SingleRowCell
// config the cell
return singleCell