Ios 为什么不是';在两个UICollectionView';我的应用程序中共享同一自定义UICollectionViewCell的?
我有一个2 UICollectionView的 一个专用于网格样式显示,另一个专用于单个文件显示。这由UISegmentedControl控制 网格样式集合视图是在interface builder中创建的:Ios 为什么不是';在两个UICollectionView';我的应用程序中共享同一自定义UICollectionViewCell的?,ios,objective-c,cocoa-touch,uicollectionview,uicollectionviewcell,Ios,Objective C,Cocoa Touch,Uicollectionview,Uicollectionviewcell,我有一个2 UICollectionView的 一个专用于网格样式显示,另一个专用于单个文件显示。这由UISegmentedControl控制 网格样式集合视图是在interface builder中创建的: -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if ([collectionView isEqual:_collec
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
另一个集合视图是在默认网格样式集合视图控制器的自定义类的viewDidLoad方法中以编程方式创建的
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setItemSize:CGSizeMake(140, 272)];
[layout setMinimumLineSpacing:1];
[layout setMinimumInteritemSpacing:1];
[layout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
_collectionView2 = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView2 setDelegate:self];
[_collectionView2 setDataSource:self];
[_collectionView2 registerClass:[VAGGarmentCell class] forCellWithReuseIdentifier:@"Cell2"];
[_collectionView2 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView2];
[_collectionView2 setHidden:YES];
}
我对这两个集合视图使用相同的自定义单元格
下面是两个集合视图都使用的数据源和委托方法
1:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
2:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
点击分段控件时会触发以下方法:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
我觉得一切都做对了。当我点击分段控件的网格样式侧时,我的主集合视图显示出来
当我点击单文件段控制选项时,结果是:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
正如您所看到的,单元格出现了,但为空。我不太清楚发生了什么事。如果单击“栅格样式段”选项,则主集合视图将显示良好
但是,尽管“单个文件集合”视图显示了正确的单元格数量,但未显示任何内容。下面应该有一个图像和一些文字
我忍不住想我错过了一步。在IB中是否有一些步骤是我在使用第二个自定义视图时没有以编程方式完成的
请注意,我还没有更改单元格大小以使集合视图2成为单文件样式。我想先让内容显示出来,然后再进一步
谢谢你的帮助
问候
更新:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
服装单元的和.h:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:_collectionView]) {
NSArray *people = [_thisController objects];
return [people count];
} else if ([collectionView isEqual:_collectionView2]) {
NSArray *people = [_thisController objects];
return [people count];
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
if ([collectionView isEqual:_collectionView2]) {
NSLog(@"collectionview 2 loaded");
static NSString *CellIdentifier = @"Cell2";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([collectionView isEqual:_collectionView]) {
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
}
return 0;
}
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setHidden:YES];
[_collectionView2 setHidden:NO];
[_collectionView2 reloadData];
} else {
NSLog(@"Grid style view selected");
[_collectionView setHidden:NO];
[_collectionView2 setHidden:YES];
[_collectionView reloadData];
}
}
#import "VAGGarmentCell.h"
@implementation VAGGarmentCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface VAGGarmentCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet PFImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *addFavouriteButton;
@property (weak, nonatomic) IBOutlet UITextView *title;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
#导入
#进口
@接口VAGGarmentCell:UICollectionViewCell
@属性(弱、非原子)IBMImageView*imageView;
@属性(弱、非原子)IBUIButton*AddFavoriteButton;
@属性(弱、非原子)IBUITextView*标题;
@属性(弱、非原子)标签*价格;
@属性(弱、非原子)IBUIActivityIndicator视图*activityIndicator;
我认为问题在于试图使用“故事板集合”视图中定义的“集合视图”2中的单元格。与其这样做,不如从storyboard collection视图中删除单元格,并将其置于xib文件中。将该单元格的类更改为VAGGarmentCell,并将所有IBOutlets连接到VAGGarmentCell类。在viewDidLoad中,为两个控制器注册nib(而不是类)
[_collectionView2 registerNib:[UINib nibWithNibName:@"VAGGarmentCell" bundle:nil] forCellWithReuseIdentifier:@"Cell2"];
[self.collectionView registerNib:[UINib nibWithNibName:@"VAGGarmentCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
您可以使用相同的重用标识符,也可以使用不同的重用标识符,只要您注册的标识符与您在celForItemAtIndexPath中为各自控制器使用的标识符相同,这并不重要。我将尝试此方法。我的收藏视图可以实现同样的功能吗?因此,我可以在XIB文件中创建第二个集合视图,这样我也可以在那里创建自定义单元格?@LondonGuy,是的,您可以在XIB文件中创建集合视图,但实际上没有任何理由这样做。在xib中创建单元就足够了。工作非常好。最终创建了第二个单元格,但使用了与此答案中描述的相同的原理。工作得很好。我怀疑单元格初始化有差异。你介意把VAGGarmentCell的代码贴出来吗?或者至少是初始化东西的部分。请记住,在IB中完成时,将调用awakeFromNib。否则,将调用
initWithStyle:reuseIdentifier:
。@MatíasR发布了更新。我的自定义单元格仅声明属性,仅此而已。这是您的问题。初始化_collectionView2的单元格时,没有任何插座连接到视图。有两种方法可以解决这个问题。。。正如@rdelmar所说,或者通过在initWithStyle:reuseIdentifier: