Reputation: 744
I am trying to select a cell in UICollectionView
, it gets selected but on scroll down it selects the some other cell on the bottom and scroll up it shows some other to be selected.
Below is the code which I am using didSelectItemAtIndexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *newIndex = indexPath;
cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];
NSString *strId = [[masterArray valueForKey:@"id"]objectAtIndex:indexPath.row];
NSString *tempIndexRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
NSLog(@"%@, %@,%d ,%@, %d", strId,tempIndexRow,cell.imageView.tag,[boolArray objectAtIndex:indexPath.row],indexPath.row);
if (strId && [[boolArray objectAtIndex:indexPath.row] isEqualToString:@"False"] && cell.imageView.tag == indexPath.row) {
cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
[boolArray replaceObjectAtIndex:indexPath.row withObject:@"True"];
}
else{
cell.selectedImage.image = Nil;
[boolArray replaceObjectAtIndex:indexPath.row withObject:@"False"];
}
}
This is what I select for the first time
This is what I get when I scroll down
Thanks
Upvotes: 1
Views: 4431
Reputation: 4671
It is not selecting another cell. Problem is mostlikely due to you reusing cells and not reintializing them correctly when they come back on the screen. When Cells not visible, it unloaded to save memory.
So better check this condtion as well in cellForItemAtIndexPath
datasource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//You cell initialization code.
if ([[boolArray objectAtIndex:indexPath.row] isEqualToString:@"True"] ) {
cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
}
else{
cell.selectedImage.image = Nil;
}
}
Upvotes: 0
Reputation: 49730
You need to set selected item index store in to one array and at cellForRowIndex
time check this array index with indexPath.row like bellow:-
selectedCellsArray
alloc this array at ViewDIdLoad
method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];
if ( [selectedCellsArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] )
{
[selectedCellsArray removeObject:[NSString stringWithFormat:@"%d",indexPath.row]];
cell.selectedImage.image = Nil;
}
else
{
[selectedCellsArray addObject:[NSString stringWithFormat:@"%d",indexPath.row]];
cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
}
}
and
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]] )
{
cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
}
else
{
cell.selectedImage.image = Nil;
}
}
Upvotes: 7