【发布时间】:2018-10-03 09:17:57
【问题描述】:
我在视图中有一个 collectionView。我确实设置了 hte 数据源、委托和一切。
这个单元正在工作:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [self.collectionAddedMembers dequeueReusableCellWithReuseIdentifier:cellReuseIdentifierAdded forIndexPath:indexPath];
cell.backgroundColor = [colores objectAtIndex:arc4random_uniform(6)];
return cell;
}
但在我的自定义单元格 AdditionalMemberCollectionViewCell 上,它不会添加新单元格:
- (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];
【问题讨论】:
-
“它不添加新单元格”是什么意思?此外,请向我们展示您如何在集合视图中注册您的
AddedMemberCollectionViewCell。 -
这意味着当我调用
[self.collectionAddedMembers reloadData];时,它不会将单元格添加到视图中。请查看我的编辑。 -
为什么投反对票?请告诉我下次学习。
标签: objective-c uicollectionview uicollectionviewcell