UICollectionview 不工作,我没有得到任何 Cell

Posted

技术标签:

【中文标题】UICollectionview 不工作,我没有得到任何 Cell【英文标题】:UICollectionview is not working, I am not getting any Cell 【发布时间】:2017-06-12 06:32:08 【问题描述】:

我正在尝试使用集合视图并重用集合视图单元格,我必须从尚未启动的服务器获取 23 张图像和名称,因为我面临的问题

这是我的自定义单元格文件集合视图文件目标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

当我移动到这个视图时,我没有得到任何单元格,它只是空白的背景图像,我什至给出了一张空白图片来查看视图的情况,但我什么也没得到,请帮助! !

【问题讨论】:

你设置了collection view的delegate和datasource吗? @Akshay :- 你注册自定义单元格了吗??.. @coder 如何注册自定义单元格?? @AravindAR 它应该交给我使用集合视图仪式的视图控制器?? 是的,我做到了,得到了红细胞,谢谢!! 【参考方案1】:

单元格未显示,因为您可能没有正确设置数据源和委托。 由于您已在情节提要中添加了单元格,因此您无需在集合视图单元格中添加图像视图作为子视图。 首先将您在storyboard中添加的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];

进行这些更改后,请重试

【讨论】:

当我创建了一个到视图控制器的出口或者我应该将它连接到自定义单元格文件时,它给出的出口无法连接以重复内容?? 是的,您需要将插座连接到自定义单元 我无法拖动它,因为该单元格属于另一个视图控制器!如何在文件中创建outlet??? 不,我无法提供与自定义 cell.hr customcell.m 文件的插座连接 您是否将您在情节提要中添加的单元格的类指定为 CustomCell ??

以上是关于UICollectionview 不工作,我没有得到任何 Cell的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionView 单元格间距不再起作用

xcode将UITableView转换为UICollectionView(无有效单元格)

UICollectionView 间距边距

UICollectionView 不显示图片[重复]

淡出 UICollectionView 中的项目

UICollectionView + AFNetworking + AutoScroll/Pagination?