我有一個對象數組,我想填充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()
}
}
你實現numberOfSections? –
不,只有numberOfItemsInSection – SalvatoreAD