22

我想展示一个 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 表。

4

5 回答 5

18

尝试这个:

// 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;
}
于 2013-09-25T18:05:30.880 回答
17

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

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
     return CGSizeMake(collectionView.frame.size.width/3.2 , 100);
}
于 2015-09-17T07:58:20.300 回答
14

在任何方向兼容的集合视图中实现 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))
 }
于 2016-03-10T14:05:47.023 回答
12

对于 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)
    }
于 2015-10-04T05:14:29.453 回答
3

假设单元格大小允许,节数指示每行中有多少个单元格。

一个部分将始终开始一个新行。

于 2014-06-07T07:00:59.427 回答