2017-06-12 41 views
0

我想使用集合视图和重用集合视图单元格,我必须从服务器得到23图像和名称,我还没有开始因为我是问题面对UICollectionview不工作,我没有得到任何单元

这是我的自定义单元格的文件集合视图文件目标C

下面是我的自定义cell.h

#import <UIKit/UIKit.h> 

@interface CustomCell : UICollectionViewCell 

{ 
    UIImageView *imageView; 
} 

@property (nonatomic, retain) UIImageView *imageView; //this imageview is the only thing we need right now. 


@end 

这里是我的自定义cell.m文件

#import "CustomCell.h" 

@implementation CustomCell 

@synthesize imageView; 

- (id)initWithFrame:(CGRect)aRect 
{ 
    if (self = [super initWithFrame:aRect]) 
    { 
    //we create the UIImageView in this overwritten init so that we always have it at hand. 
    imageView = [[UIImageView alloc] init]; 
    //set specs and special wants for the imageView here. 
    [self addSubview:imageView]; //the only place we want to do this addSubview: is here! 

    //You wanted the imageView to react to touches and gestures. We can do that here too. 
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)]; 
    [tap setNumberOfTapsRequired:1]; 
    [self addGestureRecognizer:tap]; 


    //We can also prepare views with additional contents here! 
    //just add more labels/views/whatever you want. 
    } 
    return self; 
    } 

    -(void)onButtonTapped:(id)sender 
    { 
    //the response to the gesture. 
    //mind that this is done in the cell. If you don't want things to happen from this cell. 
    //then you can still activate this the way you did in your question. 
    } 
@end 

这些都是在我的普通视图Controller.m或者文件的方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath]; 

    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 


    // create a uiview where we can place all views that need to go into this cell 
    UIView * contents=[[UIView alloc]  initWithFrame:cell.contentView.bounds]; 
    [contents setBackgroundColor:[UIColor clearColor]]; 
    [cell.contentView addSubview:contents]; 


    // set tag to the indexPath.row so we can access it later 
    [cell setTag:indexPath.row]; 

    // add interactivity 
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)]; 
    [tap setNumberOfTapsRequired:1]; 
    [cell addGestureRecognizer:tap]; 


if (cell.selected) { 
    cell.backgroundColor = [UIColor blueColor]; // highlight selection 
} 
else 
{ 
    cell.backgroundColor = [UIColor redColor]; // Default color 
} 
return cell; 
} 

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

    UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath]; 
    datasetCell.backgroundColor = [UIColor blueColor]; // highlight selection 
} 

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

UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath]; 
datasetCell.backgroundColor = [UIColor redColor]; // Default color 
} 

我没有得到任何细胞,当我提出这个观点,它只是与背景图像空白,我甚至给一张空白的图片,看看如何看,但我没有得到任何东西,请帮助!

+1

您是否设置了集合视图的委托和数据源? –

+0

@Akshay: - 你有没有注册自定义单元格? – Developer

+0

@coder如何注册自定义单元格? – Akshay

回答

1

单元格不显示,因为您可能没有正确设置数据源和委托。 由于您在故事板中添加了单元格,因此不需要将imageview作为子视图添加到集合视图单元格中。 首先将您在故事板中添加的imageview插口连接到CustomCell。然后取出

{ 
    UIImageView *imageView; 
} 

@property (nonatomic, retain) UIImageView *imageView; 

您可以删除- (id)initWithFrame:(CGRect)aRect这个方法,你可以在awakeFromNib方法

-(void)awakeFromNib { 
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)]; 
    [tap setNumberOfTapsRequired:1]; 
    [self addGestureRecognizer:tap]; 
} 

不需要做

imageView = [[UIImageView alloc] init]; 
    [self addSubview:imageView]; 

再在cellForItemAtIndexPath 删除添加点击手势初始化以下几行

[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
UIView * contents=[[UIView alloc]  initWithFrame:cell.contentView.bounds]; 
[contents setBackgroundColor:[UIColor clearColor]]; 
[cell.contentView addSubview:contents]; 

做出这些更改后,请再试一次

+0

它是给出口无法连接重复内容文我创建了一个插座视图控制器或应该我连接到自定义单元格文件? – Akshay

+0

是的,你需要连接插座到自定义单元格 –

+0

我不能拖动它,因为单元格是另一个视图控制器!如何在文件中创建插座? – Akshay

相关问题