Ios UICollectionView未显示在UICollectionView中的单元格
正如标题所示,我有一个Ios UICollectionView未显示在UICollectionView中的单元格,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,正如标题所示,我有一个UICollectionView通过IB连接到UIViewController。我有一个UICollectionView子类,名为imageCell。在viewcontrollerviewDidLoad方法中,我注册类如下: CGRect collectionViewRect = CGRectMake(384, 60, 728, 924); UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout all
UICollectionView
通过IB连接到UIViewController
。我有一个UICollectionView
子类,名为imageCell
。在viewcontrollerviewDidLoad
方法中,我注册类如下:
CGRect collectionViewRect = CGRectMake(384, 60, 728, 924);
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
imageCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:flow];
[imageCollectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"Cell"];
imageCollectionView.delegate = self;
imageCollectionView.dataSource = self;
然后在viewdideappear
中调用一个名为getPhotos
的方法(出于UI原因),该方法从服务获取照片,然后调用[imagcollectionview reloadData]代码>内部getPhotos
然后在cellForItemAtIndexPath
中执行以下操作:
imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
GRKAlbum *album = [albums objectAtIndex:indexPath.item];
cell.titleLabel.text = album.name;
return cell;
我知道我在这里没有做错任何事情,因为正在调用该方法,cell
不是nil
,也不是album
因此,当我运行所有这些代码时,会出现UICollectionView
,但其中没有任何内容。我只能看到collectionView
的背景色。任何帮助都将不胜感激
注意:我在创建时没有为imageCell
类创建XIB,我正在使用故事板。有一些方法可以调试这种情况
注册UICollectionViewCell类而不是customCell,并在cellForItem…
如果单元格出现在第一步中,请尝试使用您自己的单元格类并设置其背景
您正在使用故事板吗?然后..
您不必为集合视图注册任何单元格。您可以使用原型单元,只需为单元提供适当的id。您可以在原型单元中设计所有内容。并非所有情况下都需要自定义类 如果您在故事板中创建了单元,那么只需删除register class语句即可。如果在故事板中创建单元格,则不需要注册任何内容(对于集合视图或表视图)(事实上,如果这样做,会把事情搞砸)。您需要做的唯一一件事是确保在IB中设置标识符。您正在代码中实例化一个新的集合视图,但没有将视图添加到屏幕上。你说你已经在使用故事板了,我猜你看到的是故事板中的集合视图,它可能没有连接到数据源或委托?要么这样,要么您没有在右侧集合视图上调用重载
在故事板中配置集合视图(并为必须在代码中完成的任何操作设置一个出口),或者完全在代码中设置集合视图。目前,我认为您有两种收藏观点 我是用Xcode 6.2情节提要制作的。希望能有帮助
#import <UIKit/UIKit.h>
@interface InfoSecurity : UIViewController
@property (strong, nonatomic) IBOutlet UICollectionView *tabsCV;
@end
.m
**************
#import "InfoSecurity.h"
#import "TabsCell.h"
@interface InfoSecurity ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation InfoSecurity
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tabsCV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"TabsCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 03;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 01;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(90, 50);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = @"tabscellID";
TabsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.TabTitleLabel.text= @"OPTIONS";
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#导入
@界面信息安全性:UIViewController
@属性(强,非原子)IBUICollectionView*tabsCV;
@结束
M
**************
#导入“InfoSecurity.h”
#导入“TabsCell.h”
@接口信息安全()
@结束
@信息安全的实现
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后执行任何其他设置。
[self.tabsCV注册表类:[UICollectionViewCell类]forCellWithReuseIdentifier:@“TabsCell”];
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面
{
返回03;
}
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView
{
返回01;
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath
{
返回CGSizeMake(90,50);
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
NSString*reuseIdentifier=@“tabscellID”;
TabsCell*cell=[collectionView-dequeueReusableCellWithReuseIdentifier:reuseIdentifier-forIndexPath:indexPath];
cell.TabTitleLabel.text=@“选项”;
返回单元;
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
我用下线替换了传统的注册表单元格
行,解决了相同的问题
[self.collection registerNib:[UINib nibWithNibName:@"nibname" bundle:nil] forCellWithReuseIdentifier:identifier];
Swift 4.2版本:(感谢尼古拉·苏万兹耶夫)
self.collection.register(UINib(nibName:“nibName”,bundle:nil),forCellWithReuseIdentifier:identifier)
。因此,请确保您的导航栏(如果有)没有覆盖您正在测试的集合视图单元格。有时集合视图单元格是黑色的(与背景颜色相同,不可见,隐藏为否),重新加载可见单元格是一个解决办法。错误在于如何注册Nib/Xib文件(使用Swift 5):
使用此寄存器方法:
myCollectionView.register(UINib(nibName: "myCustomCell", bundle: nil), forCellWithReuseIdentifier: "myCustomCell")
与此相反:
myCollectionView.register(myCustomCell.self, forCellWithReuseIdentifier: "myCustomCell")
谢谢。。这是一个非常有用的解决方案。我刚刚删除了[self.collectionView注册表类:[cutomCollectionCell类]forCellWithReuseIdentifier:@“cutomCollectionCell”];哇,很好用。这个问题困扰了我半天。真不敢相信它这么容易解决。我希望下一个版本的XCode至少会显示一些警告,如果他们认为registerClass对于情节提要来说是不必要的或行为不当的话。删除registerClass
部分也帮了我的忙。谢谢!中国有句老话:教一个人如何钓鱼而不是给他鱼:)@scriptkittyya我的说法有点让人困惑。定制类并不是在所有情况下都需要的,就像你们在故事板中做的是原型单元,你们并没有太多需要定制的东西。最好有一个自定义类来更好地访问单元格属性,否则您可以通过viewWithTag
RegisterClass:Through me.:(:)这个给我修好了。任何有足够知识的人知道为什么它不仅不能像这样工作,而且完全搞砸了吗?你总是