5

有两个实体(Department <-->> Employee)。

这个部门有4名员工。当我获取该部门的所有员工时,我的日志窗口显示:

CoreData:注释:总提取执行时间:0.0017 秒,4 行。

一切都很好。

UICollectionView仅显示第一个员工 4 次。例如:

雇员 1、雇员 1、雇员 1、雇员 1

代替

员工 1、员工 2、员工 3、员工 4

我觉得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

1 回答 1

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;
}
于 2013-02-28T09:01:28.713 回答