Swift:使用Web Service的数据填充collectionView

时间:2015-10-19 14:12:52

标签: ios swift grid populate

我有一个对象数组,我想填充一个collectionview和自定义gridview(图像+文本)。 你能救我吗?

我创建了数组,当我尝试加载时,tabledata.count为0或nil

这是代码的一部分。

class BusinessController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


var tabledata:[BusinessMkp]
...

override func viewDidLoad() {
    super.viewDidLoad()
    loadCollectionViewData()

}

func loadCollectionViewData() {
    let client = WebServiceMobile()

    client.opPing(){
        (response: PingResult?, error: NSError?) -> Void in
        if response!.cpPingResult==true{
            client.opGetBusiness("", name: ""){
                (response: ArrayBusinessMkp?, error: NSError?) -> Void in
                self.tabledata=(response?.cpElencoBusinessMkp)!
                print(self.tabledata.count)
            }
        }
    }
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print(self.tabledata.count)
    return self.tabledata.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: Business_adapter = collectionView.dequeueReusableCellWithReuseIdentifier("Business_cell", forIndexPath: indexPath) as! Business_adapter
    cell.business_text.text = tabledata[indexPath.row].cpName
    //cell.business_image.image=UIImage(named: tabledata[indexPath.row].cpLogo)
    return cell
}


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    selectedItem=tabledata[indexPath.row]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

1 个答案:

答案 0 :(得分:1)

client.opPing中加载数据后,您需要

self.collectionView!.reloadData()

然后你的计数不会是零