2015-10-19 54 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

相關問題