22

正確に 2 行と各行に 100 個のセルを含む UICollectionView を表示したいと考えています。

// 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 に答える
12

Swift の場合: 1 列あたり 3 行(@SakhshiSingla Answer を使用)

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 に答える