单击UICollectionview单元格后直到我的UIImageview显示图像,直到滚动

时间:2018-04-03 09:38:28

标签: ios objective-c uitableview uiscrollview uicollectionview

我需要有关uicollectionview的帮助。我有一个带有uicollectionview水平滚动的应用程序。集合视图单元格显示来自数组的图像。当我点击任何单元格日志显示单元格索引意味着单元格被轻敲。但是该单元格上的图像没有显示在我的UIImageView上,直到我滚动collectionview。我只需要在任何单元格被点击时,点击的图像显示在我的UIimageview上而不滚动uicollectionview.Here是我的代码

(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{


    _previewCoverImage.image=_objects[indexPath.row];

  }

3 个答案:

答案 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是一个数组,您可以在其中获取图片,_magCoverViewcollectionView

答案 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) {}];

}