indexPath.row 没有从数组中获取数据
indexPath.row not getting data from the array
我创建了一个 PFQuery
以从我的解析服务器获取一些字符串并将其放入几个数组中
var packsAvailable = [""]
var packsImage = [""]
var packsDescription = [""]
如果我在查询的 for 循环中打印数组的值,我会在适当的数组中获得所有值。但是,当我尝试使用此信息填充我的 collection 视图时,没有任何反应。我无法弄清楚这一点,因为它适用于手动创建的数组 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)")
}
}
在闭包内的 for-in 循环完成后添加 collectionView.reloadData()。这将告诉您的 collectionView 获取当前数组值。
当响应来自服务器时,您忘记重新加载 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()
}*/
}
})
}
我创建了一个 PFQuery
以从我的解析服务器获取一些字符串并将其放入几个数组中
var packsAvailable = [""]
var packsImage = [""]
var packsDescription = [""]
如果我在查询的 for 循环中打印数组的值,我会在适当的数组中获得所有值。但是,当我尝试使用此信息填充我的 collection 视图时,没有任何反应。我无法弄清楚这一点,因为它适用于手动创建的数组 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)")
}
}
在闭包内的 for-in 循环完成后添加 collectionView.reloadData()。这将告诉您的 collectionView 获取当前数组值。
当响应来自服务器时,您忘记重新加载 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()
}*/
}
})
}