2009-12-29 11 views
18

साथ UITableView समस्याएं मैं कई वर्गों के साथ एक UITableView को भरने के लिए कोशिश कर रहा हूँ।cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

case 1: 
[[cell textLabel] setText:[usersTeachers objectAtIndex:(indexPath.row+1)]]; 
return cell; 
break; 

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

2009-12-28 21:09:48.380 FSS[2046:207] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:4709 
2009-12-28 21:09:48.381 FSS[2046:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 

कि सूचकांक लॉग सरणी लेकिन यह सेल ठीक से वापस नहीं करता है यह मेरा कोड है । इस प्रकार यह इस बिंदु से पहले लोड नहीं करता है। कृपया मदद करे।

उत्तर

2

यह सुनिश्चित नहीं है कि आपके पास "केस 1:" क्यों अकेले बैठे हैं, लेकिन यदि यह मामला है तो 1 से अधिक, यह गिरने जा रहा है, और इसके बाद जो कुछ भी आता है, वह वापस आ जाएगा। सुनिश्चित करें कि आप हमेशा एक सेल वापस आते हैं।

+0

मैं समझता हूँ। मेरे पास एक संपूर्ण स्विच कथन स्थापित है। मैंने बस इतना ही किया कि क्या हो रहा था इसका एक उदाहरण देने के लिए। त्रुटि देने से पहले यह एक और मामला नहीं चलाता है। –

2

आप शायद indexPath.row == 0 के लिए एक सेल वापस नहीं कर रहे हैं (अपने 'मामले 1' संदिग्ध है ...)। लेकिन जब तक आप अपना पूरा स्विच स्टेटमेंट पोस्ट नहीं करते हैं तब तक यह नहीं कह सकता है।

अपने स्विच बयान के बाहर 'सेल' लौटने का प्रयास करें, तो आप बेहतर तरीके से समझ क्या होता है। तब आपके पास कोई दावा विफलता नहीं होगी।

28

यह मेरे लिए लगता है कि आप एक आंशिक रूप से दिखाई स्विच मामले में जहां यह मामले के लिए एक सेल लौटने 1. आपका त्रुटि मुझे लगता है कि आप अपने स्विच मामले में एक अशक्त UITableViewCell कुछ शाखा लौट रहे हैं बनाता है।

सुनिश्चित करें कि स्विच मामले से बाहर सभी रास्ते एक वैध UITableViewCell ऑब्जेक्ट प्रदान करें।

आप मामले 0 के लिए क्या लौट रहे हैं? दूसरे शब्दों में, आप UITableView अनुरोधों की पहली पंक्ति के लिए क्या लौट रहे हैं?

यहाँ मेरी परियोजनाओं में से एक से कोड का एक नमूना है, यह आप एक अच्छा प्रारंभिक बिंदु देखने के लिए जहां आप गलत जा रहे हैं दे सकता है:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSInteger section = [indexPath section]; 

    switch (section) { 
     case 0: // First cell in section 1 
      cell.textLabel.text = [collectionHelpTitles objectAtIndex:[indexPath row]]; 
      break; 
     case 1: // Second cell in section 1 
      cell.textLabel.text = [noteHelpTitles objectAtIndex:[indexPath row]]; 
      break; 
     case 2: // Third cell in section 1 
      cell.textLabel.text = [checklistHelpTitles objectAtIndex:[indexPath row]]; 
      break; 
     case 3: // Fourth cell in section 1 
      cell.textLabel.text = [photoHelpTitles objectAtIndex:[indexPath row]]; 
      break; 
     default: 
      // Do something else here if a cell other than 1,2,3 or 4 is requested 
      break; 
    } 
    return cell; 
} 
+2

+1 जैसा कि आप कहते हैं, लगभग निश्चित रूप से कोड डिफ़ॉल्ट रूप से गायब है: मामला या अन्यथा कोड कोड पर शून्य वापस कर रहा है। –

+0

कृपया मेरी मदद को समझने के लिए कारण है कि हम देते हैं, तो (सेल == शून्य) अगर (सेल == शून्य) { सेल = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease]; } क्योंकि अगर सेल == शून्य या अगर (! सेल), तो यह सेल बनाने के लिए कभी भी स्थिति में नहीं जाएगा। –

+0

यदि मेरे पास डेटाबेस से 1000 मूल्य आ रहा है तो इस मामले में मुझे 1000 केस लिखना होगा ?? –

2

चेक आप में

if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

मिला

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
2

ठीक है, अगर इस प्रश्न के अन्य सभी समाधान काम नहीं करते हैं, तो सुनिश्चित करें कि सेल पहचानकर्ता

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 

अपने स्टोरीबोर्ड में सेल पहचानकर्ता इस कोड को मैच के लिए "CustomCell" होना चाहिए: तालिका दृश्य के सेल (स्टोरीबोर्ड में) के लिए सेल पहचानकर्ता आप अपने tableviewdelegate कोड में उपयोग मेल खाता है।

-3

मेरे मामले में मैं क्योंकि मैं alloc/init याद आ रही थी एक ही त्रुटि हो रही थी:

if (cell == nil) { 
     cell = [[UITableViewCell alloc] init]; 

} 
+0

यदि आप प्रश्न में कोड को देखते हैं, तो वह एक आवंटन कर रहा है लेकिन एक और विशिष्ट प्रारंभिक उपयोगकर्ता का उपयोग कर रहा है। – Abizern

+0

यह आपके काम के अनुसार काम नहीं करेगा, @Abizern सही है। –

संबंधित मुद्दे