2013-02-28 51 views
5

有兩個實體(部門 < - >>僱員)。UICollectionView僅顯示數據集中的第一項

該部門有4名員工。當我獲取本部門的所有員工時,我的日誌窗口顯示:

CoreData:註釋:總計提取執行時間:0.0017s,用於4行。

一切都很好。

但是UICollectionView只顯示第一個員工4次。 例如:

Employee1,Employee1,Employee1,Employee1

,而不是

Employee1,和Employee2,Employee3,Employee4

我有在cellForItemAtIndexPath一些錯誤的事情:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cellRecipe";  
    collectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

    Employee *emp = [array_ objectAtIndex:indexPath.item]; 
    cell.name.text = emp.name; 
    cell.image.image = emp.thumbImg; 

    return cell; 
} 

array_的NSMutableArray後取

回答

4

問題是在索引方法嘗試此代碼。

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return noOfItem/ noOfSection; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return noOfSection; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cellRecipe";  
    collectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 
    ; 

    Employee *emp = [array_ objectAtIndex:indexPath.section * noOfSection + indexPath.row]; 
    cell.name.text = emp.name; 
    cell.image.image = emp.thumbImg; 

    return cell; 
} 
+1

固定!錯誤發生在** numberOfItemsInSection **和** numberOfSectionsInCollectionView **) – Romowski 2013-02-28 09:11:44