简体   繁体   English

集合视图单元格显示不显示

[英]collection view cell display not displaying

I have a collectionview controller and a collectionviewcell. 我有一个collectionview控制器和一个collectionviewcell。 FOr the collectionviewcell I have a custom class. 对于collectionviewcell,我有一个自定义类。 I am trying to set the uiimage and it isn't working. 我正在尝试设置uiimage,但它不起作用。

Thank you in advance!. 先感谢您!。

Method in collectionview controller where I am trying to set the image. 我正在尝试设置图像的collectionview控制器中的方法。

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

    static NSString *cellIdentifier=@"Cell";

    CollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    ;
    CollectionVIewCell *feederCell = (CollectionVIewCell*)[_availableFilters objectAtIndex:indexPath.row];



    cell.labelDisplay.text=feederCell.filterName;
    NSLog(@"selected =%@",cell.labelDisplay.text);
    if (feederCell.isSelected) {
        UIImageView *imageView=[[UIImageView alloc]init];
        //process string name
        NSString*filePath =[NSString stringWithFormat:@"%@.png",feederCell.filterName];
        NSArray* words = [filePath componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
        filePath=[words componentsJoinedByString:@""];
        cell.FilePath=filePath;
        //image
        UIImage *image = [UIImage imageNamed:filePath];
        [imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        [imageView setImage:image];
        [cell setIconDisplay:imageView];//setting the image that won't set
    }else if(!feederCell.isSelected){
        UIImageView *imageView=[[UIImageView alloc]init];
        NSString*filePath =[NSString stringWithFormat:@"%@.png",feederCell.filterName];
        NSArray* words = [filePath componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
        filePath=[words componentsJoinedByString:@""];
        UIImage *image = [UIImage imageNamed:filePath];
        [imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        [imageView setImage:image];
        [cell setIconDisplay:imageView];
    }

    return cell;

.h file .h文件

//
//  CollectionVIewCell.h

#import <UIKit/UIKit.h>

@interface CollectionVIewCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *IconDisplay;
@property (strong, nonatomic) IBOutlet UILabel *labelDisplay;
@property (assign,nonatomic) BOOL isSelected;
@property (strong,nonatomic)NSString*textName;
@property(strong,nonatomic)NSString*FilePath;

@end

.m file .m文件

//
//  CollectionVIewCell.m

#import "CollectionVIewCell.h"

@implementation CollectionVIewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _labelDisplay.text=_textName;
        _IconDisplay=[[UIImageView alloc]init];
    }
    return self;
}

@end

You need to add the UIImageView as a subview. 您需要将UIImageView添加为子视图。 Otherwise it exists but isn't displayed. 否则它存在但不显示。

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

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