2015-10-19 36 views
0

我有一个对象数组,我想填充collectionview和自定义gridview(图像+文本)。 你能帮我吗?Swift:使用Web服务的数据填充collectionView

我创建阵列,并且当我尝试加载,所述tabledata.count是0或零

这是代码的一部分。

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() 
} 

}

+0

你实现numberOfSections? –

+0

不,只有numberOfItemsInSection – SalvatoreAD

回答

1

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

self.collectionView!.reloadData() 

那么你的计数不为零

+0

不工作,我尝试它,但我得到一个错误:“模糊使用'collectionView(_:numberOfItemsInSection :)'” – SalvatoreAD

+1

实现numberofsections并返回1 – PoolHallJunkie

相关问题