【问题标题】:Specify row and column number for UICollectionView为 UICollectionView 指定行号和列号
【发布时间】:2013-09-25 16:25:50
【问题描述】:

我想展示一个 UICollectionView,它正好包含 2 行,每行 100 个单元格。

// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {

    return 100;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 2;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        cell.lbl.text = @"Data";

    return cell;
}

我得到了这个:

如何为 UICollectionView 指定行号?我想创建一个 2x100 的表。

【问题讨论】:

    标签: ios uicollectionview uicollectionviewcell


    【解决方案1】:

    试试这个:

    // 1
    - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    
        return 2;
    }
    // 2
    - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
        return 100;
    }
    // 3
    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
            cell.lbl.text = @"Data";
    
        return cell;
    }
    

    【讨论】:

      【解决方案2】:

      对于每行 3 个单元格.. 添加此代码。

      - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
      {
           return CGSizeMake(collectionView.frame.size.width/3.2 , 100);
      }
      

      【讨论】:

      • 此方法需要UICollectionViewDelegateFlowLayout 协议。
      • @SakshiSingla 你为什么使用 3.2?
      • 只是计算!
      • 你能指定你是如何计算的吗,我想用同样的方法计算每行 4 个和 5 个单元格。谢谢
      【解决方案3】:

      在任何方向都兼容的集合视图中实现 n 列的更通用方法是

       func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
      
         // flow layout have all the important info like spacing, inset of collection view cell, fetch it to find out the attributes specified in xib file
         guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
              return CGSize()
          }
      
          // subtract section left/ right insets mentioned in xib view
      
          let widthAvailbleForAllItems =  (collectionView.frame.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right)
      
          // Suppose we have to create nColunmns
          // widthForOneItem achieved by sunbtracting item spacing if any
      
          let widthForOneItem = widthAvailbleForAllItems / nColumns - flowLayout.minimumInteritemSpacing
      
      
          // here height is mentioned in xib file or storyboard
          return CGSize(width: CGFloat(widthForOneItem), height: (flowLayout.itemSize.height))
       }
      

      【讨论】:

      • 您确定没有其他关键步骤可以让此方法发挥作用吗?该函数不会在这里调用。
      【解决方案4】:

      对于 Swift每列 3 行(通过使用 @SakhshiSingla 答案)

      override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
      }
      
      override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return 3
      }
      
      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
          {
              return CGSize(width: collectionView.frame.size.width/3.2, height: 100)
          }
      

      【讨论】:

        【解决方案5】:

        如果单元格大小允许,则节数表示每行中有多少个单元格。

        一个部分总是会开始一个新行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多