以下是集合视图的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
...
[_teamsCollection registerClass:[MWTeamCollectionCell class] forCellWithReuseIdentifier:@"Cell"];
}
#pragma mark data source and delegate methods of collection view
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
MWTeamCollectionCell *cell = [_teamsCollection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = (MWTeamCollectionCell*)[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.teamName.text = @"team name";
return cell;
}
当我调试行return cell;
时,我得到了正确的日志,但是我看不到这些单元格。
(gdb) po cell
<MWTeamCollectionCell: 0x72aff80; baseClass = UICollectionViewCell; frame = (0 0; 50 50); layer = <CALayer: 0x72b0090>>
(gdb) po cell
<MWTeamCollectionCell: 0x72b08e0; baseClass = UICollectionViewCell; frame = (67.5 0; 50 50); layer = <CALayer: 0x72b0970>>
(gdb) po cell
<MWTeamCollectionCell: 0x8aad380; baseClass = UICollectionViewCell; frame = (135 0; 50 50); layer = <CALayer: 0x8a72d20>>
答案 0 :(得分:1)
不需要像UITabeView那样注册类,试试这个:
[yourCollection registerClass:[MWTeamCollectionCell class] forCellWithReuseIdentifier:@"CellName"];
然后
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MWTeamCollectionCell *cell = [yourCollection dequeueReusableCellWithReuseIdentifier:@"CellName" forIndexPath:indexPath];
//Setting some thing here
return cell;
}
这就够了,记得MWTeamCollectionCell:UICollectionViewCell并在MWTeamCollectionCell.m中定制(id)init(init你的teamName标签)
UICollectionView仅适用于iOS6,如果您尝试iOS5,则无需显示
答案 1 :(得分:0)
看起来您正在分配UITableViewCell而不是UICollectionViewCell。
此外,根据您实现自定义收集单元的方式,确保已正确解码(加载)笔尖。我建议首先使用UICollectionViewCell来验证界面构建器中的出口和标识符名称是否正确。
答案 2 :(得分:0)
替换此行
[yourCollection registerClass:[MWTeamCollectionCell class] forCellWithReuseIdentifier:@"CellName"];
使用以下工作线
UINib *cellNib = [UINib nibWithNibName:@"MWTeamCollectionCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CellName"];
对我来说效果很好。