我需要有关uicollectionview的帮助。我有一个带有uicollectionview水平滚动的应用程序。集合视图单元格显示来自数组的图像。当我点击任何单元格日志显示单元格索引意味着单元格被轻敲。但是该单元格上的图像没有显示在我的UIImageView上,直到我滚动collectionview。我只需要在任何单元格被点击时,点击的图像显示在我的UIimageview上而不滚动uicollectionview.Here是我的代码
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_previewCoverImage.image=_objects[indexPath.row];
}
答案 0 :(得分:0)
点击任何单元格后,保存索引编号,然后调用[_itemsCollectionView reloadData]
,然后调用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
此方法检查单击的单元格以及indexpath.row
是否相同。如果相同,则显示: -
_previewCoverImage.image=_objects[indexPath.row];
示例:
int index = -1;
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
index = indexPath.row;
[collectionView reloadData];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == index) {
_previewCoverImage.image=_objects[indexPath.row];
} else {
// Display label with index number or whaterever you want
// or you can just ignore else part.
}
}
答案 1 :(得分:0)
经过多次尝试,我终于得到了解决方案。我只需要在reloadData
中添加didSelectItemAtIndexPath
方法。这是代码:
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_previewCoverImage.image=_objects[indexPath.row];
[_magCoverView reloadData];
}
_objects
是一个数组,您可以在其中获取图片,_magCoverView
是collectionView
。
答案 2 :(得分:0)
(: - 希望它有助于这样做//如果有任何错误让我知道。
BOOL isSelected;
unsigned short int selectedIndex;
//Declare like this in ViewController.h
//add following code in didSelectItem method
if(isSelected)
{
[self reloadCell:selectedIndex];
}
isSelected=YES;
selectedIndex=indexPath.row;
///
-(void)reloadCell:(unsigned short int) index
{
//you can call reload collection view cell here
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[serviceCollection performBatchUpdates:^{
[serviceCollection reloadItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {}];
}