Reputation: 93
I am having one array which is having 10 element. which I am able to set as item in Collection view. Now I want to add 11th element, which will be one button.
How can I do that?
Upvotes: 0
Views: 945
Reputation: 34263
The way I did this was to make
collectionView:numberOfItemsInSection:
return one more item than I actually had in my array. Then I did a test inside
collectionView:cellForItemAtIndexPath:
to see if indexPath.row == myArray.count. If so, you know it's time to add the special button cell at the end. Otherwise, you're just adding another real cell to the collection view.
- (NSInteger) collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return self->myArray.count + 1;
}
- (UICollectionViewCell*) collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.row < self->myArray.count)
{
//add regular cell to the list
}
else
{
//at the end so add special button cell to the list
}
}
I do not recommend that you simply add a dummy object to your array before rendering the collection view. This was the first method I tried. Making sure it's in the list at the right times and not in the list at the right times is a major pain. Especially if you are persisting that list of objects, it's easy to get into a situation where you accidentally persist the dummy.
Upvotes: 1
Reputation: 4019
If you are filling your collection view by your array,Then for adding an extra item or new item,just add it in your array through which you are filling your collection view by using that array in method cellForItemAtIndexPath:
,So that you would be able to include a new cell in your collectionview.
Upvotes: 0
Reputation: 75
if i understood well, you should:
However is reasonable to avoid buttons in UICollectionViews when you can use cells "as" buttons with didSelectItemAtIndexPath: method.
Upvotes: 0