2013-09-25 11 views
15

के लिए पंक्ति और कॉलम संख्या निर्दिष्ट करें, मैं एक UICollectionView दिखाना चाहता हूं जिसमें प्रत्येक पंक्ति में बिल्कुल 2 पंक्तियां और 100 कक्ष शामिल हैं।UICollectionView

enter image description here

मैं कैसे 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; 
} 

मैं इस मिल रहा है? मैं एक 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

स्विफ्ट लिए : कॉलम प्रति 3 पंक्तियाँ

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 स्तंभों को प्राप्त करने में किसी भी अभिविन्यास है (@SakhshiSingla जवाब का उपयोग करके)

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