Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView displays only first item from dataset

There are two entities (Department <-->> Employee).

There are 4 employees in this department. When I fetch all Employees in this Department my log window show:

CoreData: annotation: total fetch execution time: 0.0017s for 4 rows.

everything well.

But UICollectionView displays only first employee for 4 times. For example:

Employee1, Employee1, Employee1, Employee1

instead of

Employee1, Employee2, Employee3, Employee4

I thing there is some mistake in 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_ is a NSMutableArray after fetch

like image 673
Romowski Avatar asked Oct 05 '22 21:10

Romowski


1 Answers

Problem is in the indexing method try this code.

- (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;
}
like image 145
Hasintha Janka Avatar answered Nov 04 '22 20:11

Hasintha Janka