Dee*_*elp 1 iphone ios uicollectionview uicollectionviewcell
我正在尝试选择一个单元格UICollectionView,它被选中,但在向下滚动时,它选择底部的其他单元格并向上滚动显示其他一些要选择的单元格.
以下是我正在使用的代码 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"];
}
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次选择的

这是我向下滚动时得到的结果

谢谢
您需要将所选项目索引存储设置为一个数组,并 cellForRowIndex使用indexPath.row检查此数组索引,如下所示: -
selectedCellsArray 在ViewDIdLoad方法中分配此数组
- (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"];
}
}
Run Code Online (Sandbox Code Playgroud)
和
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4106 次 |
| 最近记录: |