【问题标题】:indexPath.row not getting data from the arrayindexPath.row 没有从数组中获取数据
【发布时间】:2016-12-20 06:35:36
【问题描述】:

我创建了一个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)")
}

}

【问题讨论】:

标签: ios arrays swift uicollectionview pfquery


【解决方案1】:

在闭包内完成 for-in 循环后添加 collectionView.reloadData()。这将告诉您的 collectionView 获取当前数组值。

【讨论】:

  • 谢谢你,这让我很头疼。
【解决方案2】:

当响应来自服务器时,您忘记重新加载 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()
                }*/
        }
    })

}

【讨论】:

  • suhit,好的答案只是将 reloadData 调用移到 for in 循环之外,或者在迭代每个对象时调用它
  • 是的,在 for 循环之后移动了 reloadData 调用
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
相关资源
最近更新 更多