loop through array items into collection view

Hi everyone


I would like to know is there anyway that we can loop through an array of items using indexPath and pass it to the collection View.

As collection view has section and row as IndexPath,is there any way to loop through a single dimension array?


Cause this can be done simply with table view, so I hope there is a solution to this.


Edit:

Just say for an example I have an array of 20 strings. If I want to loop through each of the string to be label for the table view, I use the function cellForItemAtIndexPath and I will use the indexPath.row to loop through each item eg(string[indexPath.row]). This can be done like this for tableView. However, collections seems a bit more complicated as when I use the cellForItemAtIndexPath, it doesnt return only row column as indexPath, but also the section column. So I will not be able to just loop though using eg.(string[indexPath.row]). I was wondering is there any easier method to overcome this?


Thank you

What do mean by items? How can this be done with table view?

Thanks for the reply.


I have updated my question at the top 🙂

Your question is confusing to me because table view index paths have rows and sections, collection view index paths have items and sections. Aside from the naming difference between row and item, they are otherwise exactly the same. Your question seems to be implying that collection view index paths are more complicated because they have a section, but so do table view index paths.

If you want to loop over a single section (in either kind of index path), then set the section to a constant, and vary the row/item. In other words (this is kind of pseudo-code written in the browser, so won't necessarily compile):

let mySection = 42
for myItem in 0...7 {
    let myIndexPath = NSIndexPath(forItem: myItem, inSection: mySection)
}


But, ultimately, I don't think looping over an index path is what you need to do. In your example, you say you have an array of 20 strings and you want each of those strings to be the label of an item. In that case, you want to have a class conform to the UICollectionViewDataSource protocol and use the methods in there to supply the appropriate string to the appropriate cell. The collection view will _ask_ you for the cell's content, you do not _tell_ the collection view the cells content.

CollectionView implementation of what you are referring to in the TableView is almost identical.


if you are using a reusable collection view cell and have an array of items called "myItems" the implementation would look like this:


- (UICollectionViewCell * _Nonnull)collectionView:(UICollectionView * _Nonnull)collectionView
                           cellForItemAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myIdentifier" forIndexPath:indexPath];
    cell.name = [[myItems objectAtIndex:indexPath.row] itemName];
    return cell;
}


Edit: this is using the UICollectionViewDelegate. If you are implementing a UICollectionView it will look slightly different. You will remove the collectionView argument.

loop through array items into collection view
 
 
Q