首先,很抱歉这个愚蠢的问题,但我是 CollectionView 的新手,也没什么困惑。
我正在开发UICollectionView
。我正在将图像从我的 plist
文件传递给它。我有 6 到 8 个数组项,每个数组有 6 个项(NSString)。
我正在使用这段代码..
static NSString *identifier = @"Cell";
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSArray *temp = [list objectAtIndex:indexPath.item];
UIImage *cellImage;
cellImage = [UIImage imageNamed:[temp objectAtIndex:2]];
cell.imageView.image = cellImage;
现在,当我得到标志 1 或 0 时,我想将索引更改为 objectAtIndex:5
,并且它应该针对特定项目。
UIImage *cellImage;
if (myInt == 1) {
cellImage = [UIImage imageNamed:[temp objectAtIndex:2]];
}
else{
cellImage = [UIImage imageNamed:[temp objectAtIndex:5]];
}
cell.imageView.image = cellImage;
如果我这样做,那么我会为每个项目获得 index 2
或 index 5
。是否可以根据标志获取某些项目的 index 2
和某些项目的 index 5
?或者还有其他方法吗?
任何建议将不胜感激。谢谢。
编辑:
我的 CollectionView 连续有 3 个图像(项目)。如果我点击任何图标,它将重定向到新 View 以阅读一些文档。该 View 中的栏上有一个完成按钮,它给出一个标志值 (0/1)。现在,如果我得到标志 1,那么我只想更改该情况下的 CollectionView 图标。但在这里,通过这个给定的代码,所有图标都被 index 5
替换,但我只想要 index 5
中的第一个图标和 index 2
中的其他 2 个图标>。就像只是为了区分第一个文档已被审核。
抱歉,如果造成混淆。
最佳答案
我认为您不想在这种情况下以这种方式使用 myInt 。相反,您希望在 Collection View Controller 中创建一个数组来跟踪所选的任何项目。因此,如果您有一个名为reviewedDocs的属性(指向NSMutableArray),您将添加所选单元格的indexPath。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self.reviewedDocs addObject:indexPath];
}
然后,在 cellForItemAtIndexPath: 方法中,您将根据当前的 indexPath 是否与 reviewedDocs 数组中的匹配来选择图像。
UIImage *cellImage;
if ([self.reviewedDocs containsObject:indexPath]) {
cellImage = [UIImage imageNamed:[temp objectAtIndex:5]];
}
else{
cellImage = [UIImage imageNamed:[temp objectAtIndex:2]];
}
在您返回 Collection View Controller 并重新加载数据之前,图像不会更改。我不确定您使用 myInt 标志的目的是什么,但是,例如,如果您有一个完成按钮和一个取消按钮,则您可能会分别为这两个操作返回 1 或 0。在 Collection View Controller 的 viewDidAppear 方法中,如果 myInt 为 0,则可以从 reviewedDocs 中删除最后一个对象,表明用户只是取消而不是审阅文档(然后在 Collection View 上调用 reloadData)。
关于ios - 关于 UICollectionView 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16104847/