【问题标题】:UICollectionView shows only the first itemUICollectionView 仅显示第一项
【发布时间】:2013-11-16 12:40:32
【问题描述】:

我正在做一个类似于视频专辑的项目。我使用UICollectionView 来显示这些视频的缩略图。最糟糕的是我不应该使用故事板或 xib 文件。我试图以编程方式做到这一点。我目前正在处理这段代码:

- (void)viewDidLoad
{
    i = 0;

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(self.view.frame.size.width/2.5,self.view.frame.size.width/2.5)];

    collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [collectionView setDataSource:self];
    [collectionView setDelegate:self];
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];

    [self.view addSubview:collectionView];

    [super viewDidLoad];
}

我在numberOfSectionsInCollectionView[myArray count]numberOfItemsInSection 中给出了回报。

-(UICollectionViewCell *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   UICollectionViewCell *cell = [cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];

    cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];

    imageView.image = [UIImage imageNamed:[storeData objectAtIndex:i]];

    [cell addSubview:imageView];

    i++;

    return cell;
}

我已经重新检查了myArray 中的图像。当视图加载时,集合视图仅显示第一张图像。其他 4 个单元格为空。我的代码有什么问题?

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    对于遇到此问题的人,frame 是关键。我遇到过这个并改变了:

    cell.imageView.frame = cell.frame;
    

    进入

    cell.imageView.frame = cell.bounds;
    

    操作正在使用:

    cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];
    

    这就是发生这种情况的原因。

    【讨论】:

      【解决方案2】:

      您不应该使用i 作为计数器。向您发送indexPath 的委托方法的全部意义在于它告诉您从源数据数组中获取哪些信息。因此,请删除 i 并改用 indexPath.row

      您也不需要 2 个图像视图。但是您可能应该保留您的特殊子视图,而不是使用内置在图像视图中的单元格。

      【讨论】:

      • 调试。记录图像。在图像视图周围画一个边框。
      【解决方案3】:

      您不需要计数器。正如 Wain 所说,使用indexPath.row

      重要的是,您不应该在cellForItemAtIndexPath 中创建新的子视图,而是使用此方法适当地填充它们的内容。您可以将图像视图放入情节提要原型单元格中并用标签识别它们。从dequeueReusableCellWithReuseIdentifier 返回的每个单元格都已包含图像视图。

      【讨论】:

      • 如上所述,我不应该使用情节提要。
      • 然后勾选if (cell==nil) ,然后创建一个带有子视图的单元格,否则不要再次创建它们。
      • 不好意思问,你能解释一下,我为什么要使用那个if条件,我应该在哪里使用。?我在第一行创建单元格之前使用过,但它只创建一个单元格。我对这个没有故事板的集合视图完全感到困惑。
      • 对不起,我认为您需要先对 SDK 有一些基本的了解。
      • 可能是。但我知道'if(code==nil)' 是什么意思,而且我知道如何使用它,我的问题不在于创建单元格,只是没有显示图像。您建议我以 cellForItemAtIndexPath 以外的任何方法为单元格创建子视图,但如果您知道如何操作,则可以指定如何操作。
      【解决方案4】:

      你不应该在cellForRowAtIndexPath: 中创建 ui 元素。像这样子类化一个集合视图单元:

      - (id)initWithFrame:(CGRect)frame
      {
          self = [super initWithFrame:frame];
          if (self) {
              NSLog(@"INIT WITH FRAME FOR CELL");
              //we create the UIImageView here
              imageView = [[UIImageView alloc] init];
              imageView.contentMode = UIViewContentModeScaleAspectFill;
              imageView.frame = CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3));
              [self.contentView addSubview:imageView]; //the only place we want to do this addSubview: is here!
          }
          return self;
      }
      

      然后将该子类单元格添加为属性并更改此代码:]

      [collectionView registerClass:[customCellClass class] forCellWithReuseIdentifier:@"MyCell"];
      

      执行这些更改:

      -(customCellClass *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
         UICollectionViewCell *cell = (customCellClass *)[cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
      
          cell.backgroundColor = [UIColor blackColor];
      
          imageView.image = [UIImage imageNamed:[storeData objectAtIndex:indexPath.row]];
      
          return cell;
      }
      

      最后的调整是将[super viewDidLoad] 移动到此:

      - (void)viewDidLoad
      {
           [super viewDidLoad];
           //insert the rest of the code here rather than before  viewDidLoad
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        • 2011-09-14
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多