简体   繁体   English

如何在集合视图单元格中设置多个图像?

[英]How to set multiple images in collection view cell?

I have a list of album images that i displayed in collection view. 我有一个在收藏夹视图中显示的相册图像列表。 The problem is when i select image from collection view i have to set another image on cell that is CheckMark.png image,which shows image selected check mark. 问题是,当我从集合视图中选择图像时,我必须在单元格上设置另一个图像,即CheckMark.png图像,该图像显示了选中的图像选中标记。 please help me to solve this. 请帮助我解决这个问题。

I use this code to set image but it was not working. 我使用此代码设置图像,但无法正常工作。

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

     AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];
    [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]];
 }

In your data source add property bool type for each modal. 在数据源中,为每个模态添加属性布尔类型。 Then make bool false/true at time of check click and reload data. 然后在单击和重新加载数据时将布尔值设置为false / true。

This is my method to solve this task ,You need to store boolean value No in an another array name as like checkMarkArray same as size of image array.Then you check the checkMarkArray boolean value ,if the boolean value is true set image to the imageview in the collectionView:cellForItemAtIndexPath: like this 这是我的方法来解决这个任务,你需要在另一个数组名没有存储布尔值,像checkMarkArray相同图像的大小array.Then您检查checkMarkArray布尔值,如果布尔值是true设置图像的ImageView的在collectionView:cellForItemAtIndexPath:

 [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]]

And in the collectionView:didSelectItemAtIndexPath: change value of object at index to boolean True in the checkMarkArray . collectionView:didSelectItemAtIndexPath:中,将checkMarkArray 中索引处的对象的值更改为布尔值True。

first you have to add BOOL in AlbumImageCell.h same like below code... 首先,您必须在AlbumImageCell.h中添加BOOL,就像下面的代码一样...

@property (assign, nonatomic) BOOL isCellSelected;

Then Update your code in both cellForItemAtIndexPath and didSelectItemAtIndexPath both method as per your need 然后根据需要更新cellForItemAtIndexPathdidSelectItemAtIndexPath两个方法中的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

     AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];

    if (cell.isCellSelected) {
        //add chackmark image here
        [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]];
    }else {
        //add unchak image here
        [cell.albumImage setImage:[UIImage imageNamed:@"UNCheckMark.png"]];
    }
    return cell;
}

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

    AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];

    if (cell.isCellSelected) {
        cell.isCellSelected = FALSE;
        //set unchacked image to cell
        [cell.albumImage setImage:[UIImage imageNamed:@"UNCheckMark.png"]];
    }else {
        cell.isCellSelected = TRUE;
        //set checked image to cell here like
        [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]];
    }
}

another way is store selected cell's indexpaths in array from didSelectItemAtIndexPath and check in cellForItemAtIndexPath method. 另一种方法是将所选单元格的索引路径从didSelectItemAtIndexPath存储在数组中,然后检入cellForItemAtIndexPath方法。 but Bool is very easy insted of this. 但是布尔很容易做到这一点。 Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM