我已经创建了一个PFQuery
来从我的解析服务器获取一些字符串并将其放在几个数组中
var packsAvailable = [""]
var packsImage = [""]
var packsDescription = [""]
如果我在查询的for循环中打印数组的值,我会得到一个正确数组中的所有值。但是,当我尝试使用此信息来填充我的集合视图时,没有任何反应。我无法解决这个问题,因为它适用于我已经注释用于测试的手动创建的数组tableImages和tableData。
import UIKit
import Parse
private let reuseIdentifier = "Cell"
class CollectionViewController: UICollectionViewController {
//var tableData: [String] = ["Tis is a description", "Test 22", "Test 33"]
//var tableImages: [String] = ["walk1bg", "2", "walk3bg"]
var packsAvailable = [""] // the total packs in the app
var packsImage = [""]
var packsDescription = [""]
override func viewDidLoad() {
super.viewDidLoad()
let packQuery = PFQuery(className: "Pack")
packQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let packs = objectsArray {
self.packsAvailable.removeAll() // remove them incase they double up
print(packs.count)
for object in packs {
/*
print(object["packName"])
print(object["packImage"])
print(object["packDesctription"])
*/
self.packsAvailable.append(object["packName"] as! String)
self.packsImage.append(object["packImage"] as! String)
self.packsDescription.append(object["packDesctription"] as! String)
/*
print(self.packsAvailable)
print(self.packsImage)
print(self.packsDescription)
*/
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return packsAvailable.count
}
func UIColorFromHEX(hexValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((hexValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((hexValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(hexValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
//cell.labelCell.text = tableData[indexPath.row]
//cell.imageCell.image = UIImage(named: tableImages[indexPath.row])
cell.labelCell.text = packsDescription[indexPath.row]
print(packsDescription[indexPath.row])
cell.imageCell.image = UIImage(named: packsImage[indexPath.row])
cell.imageCell.layer.masksToBounds = true
cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height/2
cell.imageCell.layer.borderWidth = 3
cell.imageCell.layer.borderColor = UIColorFromHEX(hexValue: 0x62aca2).cgColor
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Selected \(indexPath.row)")
}
}
答案 0 :(得分:2)
在封闭内完成for-in循环后添加collectionView.reloadData()。这将告诉collectionView获取当前数组值。
答案 1 :(得分:1)
当响应来自服务器时,您忘记重新加载collectionView。只需在完成for循环后添加collectionView.reloadData()
override func viewDidLoad() {
super.viewDidLoad()
let packQuery = PFQuery(className: "Pack")
packQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let packs = objectsArray {
self.packsAvailable.removeAll() // remove them incase they double up
print(packs.count)
for object in packs {
/*
print(object["packName"])
print(object["packImage"])
print(object["packDesctription"])
*/
self.packsAvailable.append(object["packName"] as! String)
self.packsImage.append(object["packImage"] as! String)
self.packsDescription.append(object["packDesctription"] as! String)
/*
print(self.packsAvailable)
print(self.packsImage)
print(self.packsDescription)
*/
}
//swift 2.3
dispatch_async(dispatch_get_main_queue()) {
self.collectionView.reloadData()
}
//swift 3
/* DispatchQueue.main.async {
self.collectionView.reloadData()
}*/
}
})
}