2013-09-25 39 views
15

我想顯示一個UICollectionView,其中每行包含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; 
} 

我得到這個:

enter image description here

我如何可以指定一個行號爲UICollectionView?我想創建一個2x100表格。

回答

11

試試這個:

// 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; 
} 
+0

非常感謝您... – onivi

+42

看起來不正確 – aelam

+0

使用自定義佈局更好:http://stackoverflow.com/a/35362818/833885 – brianLikeApple

2

部分的數量表明許多細胞如何將每一行中,假設電池的尺寸允許它。

一節將始終啓動一個新行。

13

對於每行3個單元格..添加此代碼。

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

此方法需要'UICollectionViewDelegateFlowLayout'協議。 –

+1

是的,我們確實需要。看看:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/ –

+0

這應該是每行3個單元格,或多少列。對? – malajisi

9

的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) 
    } 
9

的更通用的方法,以實現在集合視圖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)) 
}