[英]Image not showing on my UIImageview after clicking on UICollectionview cell until scroll
i need help regarding uicollectionview.I have an app with uicollectionview horizontal scrolling.The collectionview cell display images from array.when i clicked on any cell log show the cell index means cell get tapped.But the image on that cell did not display on my UIImageView until i scroll the collectionview.I just need when any cell get tapped, the tapped image show up on my UIimageview without scrolling the uicollectionview.Here is my Code 我需要有关uicollectionview的帮助。我有一个uicollectionview水平滚动的应用程序.collectionview单元格显示来自array的图像。当我单击任何单元格日志时,显示单元格索引表示该单元格被点击。但是该单元格上的图像未显示在我的计算机上直到我滚动collectionview.UIImageView.I只需要当任何单元格被点击时,点击的图像显示在我的UIimageview上,而无需滚动uicollectionview。这是我的代码
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_previewCoverImage.image=_objects[indexPath.row];
}
Once you click on any cell, save the index number and then call [_itemsCollectionView reloadData]
and then in 单击任何单元格后,保存索引号,然后调用[_itemsCollectionView reloadData]
,然后在
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
this method checks the clicked cell and whether indexpath.row
is the same or not. 此方法检查单击的单元格以及indexpath.row
是否相同。 If it's the same, then display:- 如果相同,则显示:-
_previewCoverImage.image=_objects[indexPath.row];
Example: 例:
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.
}
}
After multiple trying I finally got the solution. 经过多次尝试,我终于找到了解决方案。 I just need to add the reloadData
method in didSelectItemAtIndexPath
. 我只需要在didSelectItemAtIndexPath
添加reloadData
方法。 Here is the code: 这是代码:
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_previewCoverImage.image=_objects[indexPath.row];
[_magCoverView reloadData];
}
_objects
is an array where you get the images and _magCoverView
is a collectionView
. _objects
是获取图像的数组, _magCoverView
是collectionView
。
(:-Hope it helps do like this//if any mistakes let me know guys. (:-希望它有助于这样做///如果有任何错误会让我认识大家。
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) {}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.