我正在发送文本和图片进行解析,我想从解析中检索它们并分配到集合视图。标题视图中标签的字符串和集合视图的图像。到目前为止,这是我的代码:
class Header: UICollectionReusableView {
@IBOutlet weak var groupName: UILabel!
}
class ImagesCell: UICollectionViewCell {
@IBOutlet weak var imageNew: UIImageView!
}
let reuseIdentifier1 = "Cell"
class TheNewCollectionViewController: UICollectionViewController {
@IBOutlet var imagesCollection: UICollectionView!
var allImages: NSMutableArray = []
var imagesFile: NSArray = NSArray()
var groups: NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
queryParse()
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesFile.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier1, forIndexPath: indexPath) as! ImagesCell
let imageObject = imagesFile.objectAtIndex(indexPath.row) as! PFObject
let filedImage = imageObject.objectForKey("imageFile") as! PFFile
filedImage.getDataInBackgroundWithBlock { (data, error) -> Void in
if(error != nil) {
cell.imageNew.image = UIImage(data: data!)
self.imagesCollection.reloadData()
}
}
return cell
}
func queryParse() {
let query = PFQuery(className: "images")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error != nil && objects?.count != 0) {
self.imagesFile = NSArray(array: objects!)
self.imagesCollection.reloadData()
} else {
print("Error: \(error)")
self.imagesCollection.reloadData()
}
}
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
let objectQuery = PFQuery(className: "images")
objectQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error != nil {
headerView.groupName.text = objects.objectForKey("groupName") as! String //here i'm getting an error
} else {
print("Error: \(error)")
}
}
return headerView
}