我对Swift和Parse相当新,并且我正在尝试使用PFQueryCollectionViewController来加载一组图像和标题。问题是我的PFQueryCollectionViewCell在模拟器中复制了相同的图像和文本标签,尽管我在查询的类名中有多个对象。我搜索了多个讨论主题,但似乎无法找到解决它的解决方案。它可能是我看到的超级简单的东西,但我的眼睛开始流血!
提前感谢您的帮助。
FeedViewController(PFQueryCollectionViewController)
import Foundation
import UIKit
import Parse
import ParseUI
import Bolts
let reuseIdentifier:String = "feedCell"
class FeedViewController: PFQueryCollectionViewController, UICollectionViewDelegateFlowLayout {
let sectionInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryCollectionView
self.parseClassName = "Products"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 50
}
override func queryForCollection() -> PFQuery {
var query = PFQuery(className: "Products")
query.orderByAscending("createdAt")
return query
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> PFCollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? FeedViewCell
let item = indexPath.item
for object in objects {
cell?.parseObject = object[item] as? PFObject
if let parseObject = object["url"] as? String? {
cell?.productImage?.image = nil
if var urlString: String? = parseObject as String? {
var url:NSURL? = NSURL(string: urlString!)
if var url:NSURL? = NSURL(string: urlString!) {
var error:NSError?
var request:NSURLRequest = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5.0)
NSOperationQueue.mainQueue().cancelAllOperations()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
(response:NSURLResponse!, imageData:NSData!, error:NSError!) -> Void in
cell?.productImage?.image = UIImage(data: imageData) } )}
} }
cell?.title?.text = object["title"] as? String
var text = object["title"] as? String
}
return cell!
}
override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath ) {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath)
}
override func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 170, height: 300)
}
override func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return sectionInsets
}
}
FeedViewCell(PFQueryCollectionViewCell)
import Foundation
import UIKit
import Parse
import ParseUI
class FeedViewCell: PFCollectionViewCell {
@IBOutlet weak var productImage: PFImageView?
@IBOutlet weak var title: UILabel?
var parseObject:PFObject?
}