2015-10-19 3 views
0

У меня есть массив объектов, и я хочу заполнить коллекцию и пользовательский gridview (изображение + текст). Вы можете мне помочь?Swift: Populate 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

нет, только номерOfItemsInSection – SalvatoreAD

ответ

1

После загрузки данных в client.opPing вам нужно

self.collectionView!.reloadData() 

, то ваш счетчик не будет нулевой

+0

не работает, я пытаюсь, но получаю сообщение об ошибке: «Неоднозначное использование 'collectionView (_: numberOfItemsInSection :)'" – SalvatoreAD

+1

реализовать числоofsections и return 1 – PoolHallJunkie

Смежные вопросы