UICollectionView: issue many cells are selected when I only select one cell - ios

I faced an issue an take lot of time but cannot figure out the issue:
I created a UICollectionView with very simple content, one lable to indicate cell_number, and when a cell is selected, the background of cell is changed to orange.
I create a custom cell with xib file for design (very simple one). And I load this cell from my UICollectionView in my DemoUICVViewController.m file normally as many people did with some steps like:
- declare the custom cell class in ViewDidload:
[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:#"MyCell"];`
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell * myCell = (MyCustomCell*)[cv dequeueReusableCellWithReuseIdentifier:#"myCell" forIndexPath:indexPath];
myCell.lable.text = [arrayNumber objectAtIndex:indexPath.item];
return myCell;
}
and when selected:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the cellForItemAtIndexPath and set the background coler of cell to orange
}`
Number of section I return 1, and number of item in section I return 10
All the cells in UICollectionView are display correctly. But when I tap to select a cell I see not only that cell is orange, but also some other cells also change to orange for e.g cell 5 and cell 9.
I debug but the didSelectItemAtIndexPath only get called once and at the right index, very strange when some other cells also be orange.
I am not sure if the problem is of Reusability of cell identifier, could someone give me some advise.
I attached photo for e.g here:
Image describes

Finally I found the root cause to fix: add this line to check to
collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
...
if(myCell.selected)
// set background of cell to orange;
else
// set white background;
...
}

Related

collectionviews in tableview selection

i added my UICollectionView to the UITableViewCell (objective c), and the data is showing correctly in the collectionviews inside the tableview, the selection in the same collectionview is working as it expected ; the problem i have is:
if the user select one cell in collectionview (a) the label backgroundcolor of this cell is set to red
when the user select one cell in collectionview (b) the label backgroundcolor of this cell is set to red but the backgroundcolor of the previous selected cell in collectionview a is still
in red !
my question is how can i set clearColor to the previous selected cell ?
please help thank you
nb: the data and the number of collectionview are dynamic
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if(self.selectedIndex == indexPath.item)
{
cell.label.backgroundColor = [UIColor redColor];
}
else
{
cell.label.backgroundColor=[UIColor clearColor];
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
self.selectedIndex = indexPath.row;
}
Once you record or mark the state in UI, sometimes it's not simple way.
I suggest store the state in data model, and the cell's appearance is based on the data model, once the data model change, refresh the corresponding cell, that will work.
Here is a simple example:
in ViewController.m
in XXCellDataModel.h
in XXCollectionViewCell.h
in XXCollectionViewCell.m

How to change button image for all UICollectionView cells?

I am using a UICollectionView to display some data. I am having one button on each cell. Now on the button click of at some specific index(ex: 9) I want to change buttons image for all cells. Can some one suggest how to do that?
I would loop through all the visible cells and change the image. For example:
for (CustomCellClass *cell in collectionView.visibleCells) {
[cell.button setImage:[UIImage imageNamed:#"new image"] forState:UIControlStateNormal];
}
For off-screen cells, I would make sure to set the new image in collectionView:cellForItemAtIndexPath: as the user scrolls to those cells.
We can simply add the logic in cellForItemAtIndexPath like this.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (self.buttonClicked) { // boolean check
[cell.newButton setImage:buttonClickedImage];
}else {
[cell.newButton setImage:buttonNotClickedImage];
}
return cell;
}
After clicking the button, you can also just reload all visible cells.
[self.collectionView reloadItemsAtIndexPaths:self.collectionView.indexPathsForVisibleItems];
Feel free to suggest edits to make this answer better :)

UIImageView in UICollectionView cells not displaying

I'm very new to iOS development, sorry for posting such a basic mistake, I just can't get my head around this.
I will tell you what I have done so far, hopefully someone can help me out here. I have not edited any code so far.
Add collection view controller (click and drag into main.storyboard)
Configure cell sizes in the attributes editor of the CollectionView
Drag UIImageView object onto each cell
Import my images into the project (drag images into sidebar and import)
Select each UIImageView and in attributes select the correct image
Having done this, my images display perfectly in the storyboard editor but when I run the app in the simulator it is just a blank screen with my specified background colour.
Screenshot of editor, Screenshot of simulator
You should add the data source method after creating a collection view controller class,
// Defines the number of cells in a section of collection view
- (NSInteger)collectionView:(UICollectionView *)cv numberOfItemsInSection:(NSInteger)section;
{
return numberOfItems;
}
// Defines the cells in the collection view
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
// Gallerycell is the custom collection view cell class holds the UIImage View
GalleryCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"GalleryCell" forIndexPath:indexPath];
// getting the image
cell.cellImage.image = [UIImage imageNamed:[dataSourceArray objectAtIndex:indexPath.row]];
return cell;
}
You should implement datasource methods for displaying a cell. Doesn't meter if it's static or dynamic cells.

iOS: How to start populating cells in UICollectionview at the half way mark

I want to start populating my UICollectionView with my cells at the middle and not at the very top. I am not sure if this bit of code will help, but I will post it just in case.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = (Cell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
NSMutableArray *data = [sections objectAtIndex:indexPath.section];
cell.cellImg.image = [data objectAtIndex:indexPath.item];
return cell;
}
Why not populate your array in another order? Take a look on Apple UICollectionViewDataSource Reference:
"You do not need to set the location of the cell inside the collection
view’s bounds. The collection view sets the location of each cell
automatically using the layout attributes provided by its layout
object."
If you need some more info about Collection View Layout:
UICollectionview Layout Tutorial

Why when my custom cells in CollectionView are clicked they do not get highlighted?

When I click they,the function of click(shch as show an alert or get into a new view) works fine but the appearance of the cell I clicked don't change.What I can do to make the cell I click get highlighted ?THX~
Add a public method performSelectionAnimations to the definition of MyCollectionViewCell. Then call it from collectionView:didSelectItemAtIndexPath:
-(void)performSelectionAnimations {
...
...
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell performSelectionAnimations];
}
Notice I've taken out the call to [cell setSelected:YES], since that should already be taken care of by the UICollectionView.
You must do it programmatically. Look at :
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CollectionViewPGforIOS.pdf#page18
When you tap on the cell the highlight state change to YES, but you must implement de behaviour (i.e. change background cell on click).

Resources