jaypee
jaypee

Reputation: 75

iOS UICollectionView and CoreData

I'm new to iOS and still trying to wrap my head around the basics (my newbie disclaimer). I'm starting to play with persistent data and presenting this data with uicollectionview.

I can create cells and the right number with arrays, I'm having issues with the right number of cells interacting with core data. I'll try an example to clarify: i have an entity person with attributes first name and last name. Say i have 1 person saved. When i get my number of objects in section, i get 1 object for my 1 person. This number is used to create # of cells which is 1 for my 1 person. I'd like to create 2 cells (one for first name and one for last name) for each person, but i can't figure that out. I tried putting each entity into its own section and then using arithmetic to return 2 * #of entities in section, but hat crashes.

This seems rather simple but i can't figure it out?

Upvotes: 0

Views: 433

Answers (1)

Mundi
Mundi

Reputation: 80271

In numberOfSectionsInCollectionView::

return 1; 

In collectionView:numberOfItemsInSection::

return fetchedData.count*2;

In collectionView:cellForItemAtIndexPath::

Person *p = fetchedData[indexPath.row/2]; // integer division
...
cell.textLabel = indexPath.row % 2 ? 
        p.firstName     // odd row number
       :p.lastName;     // even row number

Upvotes: 1

Related Questions