2012-12-13 11 views
6

से UICollectionView के आइटम/सेल का चयन कैसे करें मैंने अपने ऐप में UICollectionView लागू किया है। मेरी समस्या यह है कि मुझे चयन करने की आवश्यकता है (जैसे कि उपयोगकर्ता ने उस पर टैप किया है) एक सेल programmatically। विधि:कोड

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
        animated:(BOOL)animated 
       scrollPosition:(UICollectionViewScrollPosition)scrollPosition 

कि UICollectionView वर्ग का हिस्सा है मैं क्या कॉल करने की आवश्यकता है, क्योंकि यह विधि कॉल नहीं करता नहीं है:

- (void)collectionView:(UICollectionView *)collectionView 
     didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

यह सिर्फ करने के लिए सेल की selected संपत्ति सेट YES;

+2

आपके सेल को चुनने के बाद विधि को कॉल करने के बारे में क्या? आप [self collectionView: yourView didSelectItemAtIndexPath: yourIndexPath] को कॉल कर सकते हैं; –

+0

हां। समझ गया। धन्यवाद! –

+1

शायद यह उत्तर आपको मदद करेगा http://stackoverflow.com/questions/13177201/select-items-programmatically-in-uicollectionview –

उत्तर

7

मैं प्रोग्राम को सेल से चुनने के लिए [self collectionView:yourView didSelectItemAtIndexPath:yourIndexPath] पर कॉल कर रहा हूं। लेकिन विधि सेल में हमेशा शून्य है। जब उपयोगकर्ता सेल चुनता है तो यह पूरी तरह से अच्छा काम करता है।

+7

या तेजी से आप इस्तेमाल कर सकते हैं: 'collectionView.delegate .collectionView (collectionView, didSelectItemAtIndexPath: someIndexPath)?!' – mbuff24

+0

काम करता है यही कारण है, यह एक बुरा व्यवहार हालांकि प्रतिनिधि तरीकों सीधे कॉल करने के लिए (संग्रह देखें कि विधि पर आह्वान करना चाहिए इसके ऐप्पल के रूप में प्रतिनिधि सिफारिश करता है)। जैसा कि @leviathan ने उल्लेख किया है "[selectItemAtIndexPath: एनिमेटेड: scrollPosition:]" प्रतिनिधि विधि का आह्वान नहीं करेगा, इसलिए इसके आसपास काम करने के बेहतर तरीके हैं। – ppalancica

7

सबसे पहले, आपको लक्षित सेल दृश्यमान बनाने की आवश्यकता है, अन्यथा [yourCollectionView cellForItemAtIndexPath:yourIndexPath] हमेशा शून्य लौटाता है।

// scroll to the cell 
[yourCollectionView scrollToItemAtIndexPath:yourIndexPath 
          atScrollPosition:UICollectionViewScrollPositionBottom 
            animated:NO]; 

// call delegate method 
[self collectionView:yourCollectionView didSelectItemAtIndexPath:yourIndexPath]; 

// now you have selected the cell and can do anything to it in the delegate method :-) 
+0

मेरा सेल हमेशा दिखाई देता है, लेकिन cellForItemAtIndexPath हमेशा शून्य लौटाता है (कॉल स्क्रॉल के बाद भी ITememAtIndexPath –

8

हां, यह उचित व्यवहार है। [selectItemAtIndexPath:animated:scrollPosition:]says के लिए प्रलेखन:

इस विधि के लिए किसी भी चयन से संबंधित प्रतिनिधि तरीकों का कारण नहीं है कहा जा।