2013-02-25 39 views
20

संग्रह विचारों के साथ पहली डुबकी ले रहा है और इस त्रुटि में चल रहा हूँ:त्रुटि तरह के एक दृश्य के विपंक्ति नहीं कर सका UICollectionElementKindCell

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

कोड बहुत सरल है, जिन्हें आप नीचे। मैं अपने जीवन के लिए यह नहीं समझ सकता कि यह क्या है कि मैं याद कर रहा हूं।

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
cell.backgroundColor = [UIColor whiteColor]; 
return cell; 
} 

संग्रह दृश्य नियंत्रक एक निब और प्रतिनिधियों & datasources दोनों फ़ाइल के मालिक की तैयारी में हैं का उपयोग कर बनाया गया था।

देखें नियंत्रक की हेडर फ़ाइल भी वास्तव में मूलभूत है।

@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> 
@end 
+11

[स्वयं।संग्रह दृश्य रजिस्टर क्लास: [UICollectionViewCell क्लास] के लिए CellWithReuseIdentifier: @ "सेल"]; –

उत्तर

28

विपंक्ति विधि के लिए UICollectionView documentation से:

Important: You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.

+0

मुझे क्या करना चाहिए यदि मैं डिफॉल्ट UICollectionViewCell का उपयोग कस्टम नहीं करता – Mani

+0

आप कस्टम क्लास के बजाय बेस क्लास पंजीकृत करते हैं। लेकिन आप लगभग हमेशा एक उपclass चाहते हैं। डिफ़ॉल्ट सेल में इसमें कुछ भी नहीं है। – jrturton

+0

हां, इसे धन्यवाद @ jrturton – Mani

6

पूरक क्या लिखा @jrtuton ... तुम क्या जरूरत है:

1) अपने निब फ़ाइल यह "कनेक्ट" के लिए रजिस्टर आपके पहचानकर्ता के साथ:

//MyCollectionView.m 
- (void) awakeFromNib{ 
[self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil] forCellWithReuseIdentifier: @"MyCellIdentifier"]; 
} 

2) अपने कस्टम सेल को लोड करने के लिए अपने पहचानकर्ता का उपयोग करें एक निब से:

//MyCollectionView.m 
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath]; 
} 

3) का प्रयोग करें हमेशा स्थिर NSString * एक ही पहचानकर्ता फिर अपने अनुप्रयोग में से बचने के लिए।

21

आपको dequeueReusableCellWithReuseIdentifier के तर्क और UICollectionViewCell की संपत्ति के बीच समान पहचानकर्ता का उपयोग करने की आवश्यकता है।

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath]; 

identifier

+0

धन्यवाद !!!!!! –

+0

UITableViewCell के लिए पहचानकर्ता को कैसे पंजीकृत करें UICollectionView जोड़ा गया? – kiran

1

मैं जानता हूँ कि यह काफी पुराना पुराना है, लेकिन मैं एक ही समस्या का सामना करना और साझा करने के लिए क्या यह मेरे लिए तय चाहता था।

मेरे मामले में, मैं एक वैश्विक पहचानकर्ता

let identifier = "CollectionViewCell" 

की घोषणा की और उपयोग करने के लिए स्वयं लिया है सही उपयोग करने से पहले:

collectionView.dequeueReusableCellWithReuseIdentifier(self.identifier, forIndexPath: indexPath) as! CollectionViewCell 

आशा इस मदद करता है किसी को

:)
-1

यदि आप इसे कोड द्वारा बनाते हैं, तो आपको एक कस्टम UICollectionViewCell बनाना होगा, फिर, init a UICollectionView, और दृश्य सेट करने के लिए loadView का उपयोग करें UICollectionView जो आप बनाते हैं। यदि आप viewDidload() में बनाते हैं, तो यह काम नहीं करता है। इसके अतिरिक्त, आप UICollectionViewController भी खींच सकते हैं, यह बहुत समय बचाता है।

0

आपको स्टोरीबोर्ड में सही पुन: उपयोग करने वाला पहचानकर्ता देना होगा, जिसे आपने संग्रह ViewCell दर्ज करते समय कोड में दिया है।

यहां कोड है।

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ClctnVC", for: indexPath)as! ClctnVC 
    return cell    
}