我在视图中有一个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];
答案 0 :(得分:0)
您确定该单元格未真正添加到您的收藏夹视图中吗?
但是,您做得不好,因为调用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