Objective c UICollection视图-不添加新的自定义单元格

Objective c UICollection视图-不添加新的自定义单元格,objective-c,uicollectionview,uicollectionviewcell,Objective C,Uicollectionview,Uicollectionviewcell,我在一个视图中有一个collectionView。我设置了数据源、代理和所有内容 此单元格正在工作: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [self.collectionAddedMembers dequeueReus

我在一个视图中有一个collectionView。我设置了数据源、代理和所有内容

此单元格正在工作:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [self.collectionAddedMembers dequeueReusableCellWithReuseIdentifier:cellReuseIdentifierAdded forIndexPath:indexPath];
    cell.backgroundColor = [colores objectAtIndex:arc4random_uniform(6)];
    return cell;
}
但在我的自定义单元格AddedMemberCollectionViewCell上,它不会添加新单元格:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AddedMemberCollectionViewCell *cell = [self.collectionAddedMembers dequeueReusableCellWithReuseIdentifier:cellReuseIdentifierAdded forIndexPath:indexPath];
    NSArray *firstSection = [selectedMembersData objectAtIndex:0];
    SelectedContacto *cellData = [firstSection objectAtIndex:indexPath.row];
    NSMutableDictionary *dataForConfigure = [[NSMutableDictionary alloc] init];
    [dataForConfigure setObject:cellData.contactAvatar forKey:@"contactAvatar"];
    [dataForConfigure setObject:cellData.contactName forKey:@"contactName"];
    [dataForConfigure setObject:cellData.memberId forKey:@"contactName"];
    [cell configure:dataForConfigure];
    return cell;
}
这是自定义单元格的代码

.h文件:

@interface AddedMemberCollectionViewCell : UICollectionViewCell
@property (nonatomic) IBOutlet UIImageView *avatar;
@property (nonatomic) IBOutlet UILabel *memberName;
@property (nonatomic) IBOutlet UIButton *removeMember;
@property (nonatomic) NSNumber *memberId;
- (void) configure:(NSDictionary*)cellData;
@end
.m文件

#import "AddedMemberCollectionViewCell.h"

@implementation AddedMemberCollectionViewCell
- (void) configure:(NSDictionary*)cellData {
    self.avatar = [cellData objectForKey:@"contactAvatar"];
    self.memberName = [cellData objectForKey:@"contactName"];
    self.memberId = [cellData objectForKey:@"memberId"];
}
@end
我可以提供更多的代码,但我认为没有必要。这里发生了什么

编辑:

在viewDidLoad中注册:

self.searchBar.delegate = self;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.collectionAddedMembers.dataSource = self;
self.collectionAddedMembers.delegate = self;
[self.collectionAddedMembers registerClass:AddedMemberCollectionViewCell.class forCellWithReuseIdentifier:cellReuseIdentifierAdded];

确实没有将单元格实际添加到集合视图中吗

但是,您做得不对,因为调用
dequeueReusableCellWithReuseIdentifier
不会创建
UICollectionViewCell
,而是重复使用现有的单元格,因此
init
中的初始化代码将永远不会被调用

一个好的方法应该是这样的:

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

    NSDictionary *cellData = [data objectAtIndex:indexPath.row];

    // Call custom cell's configure function to init cell content
    [cell configure:cellData];
    return cell;
}

@interface CustomCell : UICollectionViewCell
-(void)configure:(NSDictionary *)data;
@end

@implementation CustomCell

-(void)configure:(NSDictionary *)data {
    // Init your cell content
}

@end

“它不增加新的细胞”确切地说是什么意思?另外,向我们展示如何在集合视图中注册您的
AddedMemberCollectionViewCell
。这意味着当我调用
[self.collectionAddedMembers reloadData]时它不会将单元格添加到视图中。请看我的编辑。为什么投否决票?请告诉我下次学习的内容。NSLogs显示数组中的
元素:1 |单元格:0
调用
self.collectionAddedMembers.visibleCells.count
。我用同样的结果实施了你的方法。可能正在添加单元格,但它们不可见。