【问题标题】:cells in collection view are not visible集合视图中的单元格不可见
【发布时间】:2013-01-16 09:47:46
【问题描述】:

这里是集合视图的代码:

- (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>>

【问题讨论】:

    标签: iphone ios objective-c uicollectionview uicollectionviewcell


    【解决方案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 中自定义(初始化你的团队名称标签)

    而且 UICollectionView 只适用于 iOS6,如果你尝试 iOS5,则没有任何显示

    【讨论】:

      【解决方案2】:

      看起来您分配的是 UITableViewCell 而不是 UICollectionViewCell。

      此外,根据您实现自定义集合单元的方式,确保笔尖已正确解码(加载)。我建议首先使用 UICollectionViewCell 来验证您在界面生成器中的插座和标识符名称是否正确。

      【讨论】:

      • 我该怎么做:验证您在界面生成器中的插座和标识符名称是否正确?
      • 你是对的 :) 我确实分配了 UITableViewCell。这是我的新代码(它仍然不起作用:( ) -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; MWTeamCollectionCell *cell = [ _teamsCollection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.teamName.text = @"team name"; return cell; }
      【解决方案3】:

      替换这一行

      [yourCollection registerClass:[MWTeamCollectionCell class] forCellWithReuseIdentifier:@"CellName"]; 
      

      下面的工作线

      UINib *cellNib = [UINib nibWithNibName:@"MWTeamCollectionCell" bundle:nil];    
      [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"CellName"];
      

      对我来说效果很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 2015-07-06
        相关资源
        最近更新 更多