2013-08-23 11 views
5

अपने आवेदन में, मैं tableview से हटाने की पंक्ति के बाद मेरे tableview ([tablView reloadData];) को फिर से लोड तो canEditRowAtIndexPath विधि alway (प्रवेशक) कुल पंक्तियों की संख्या के लिए कहते हैं।canEditRowAtIndexPath विधि पर, UITableView का reloadData ठीक से काम नहीं करता है?

उदाहरण के लिए:

मैं अपने tableview पर पंक्तियाँ है, तो मैं पंक्ति tableView से हटा दें। हटाने के बाद, मैं फिर से लोड मेरी tableview ([tablView reloadData]) लेकिन canEditRowAtIndexPath विधि कॉल 5 समय बजाय 4 बार ??

Terminating app due to uncaught exception 'NSRangeException', reason: '* **-[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'

मैं भी कुछ देरी (NSTimer का प्रयोग करके) के बाद तालिका को फिर से लोड करने की कोशिश की लेकिन यह भी मेरे लिए काम नहीं किया:

तो मैं हमेशा त्रुटि के बाद मिला है।

मैं कुछ कोड यहाँ रखा:

मैं विशेष पंक्ति पर canEditRowAtIndexPath होता है कि कौन @"status" isEqualToString:@"directory" इस तरह की तरह,

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%d", self.listOfSounds.count); 
    // Return NO if you do not want the specified item to be editable. 
    if([[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"status"] isEqualToString:@"directory"]) 
     return YES; 
    else 
     return NO; 
} 

हटाने पंक्ति के कोड:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"main_id"]]; /// delete record from DB 
     [self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array 
     [self updateListofSoundsFile]; /// Custom method 
    } 
} 

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return NO; // i also tried to return YES; 
} 

यहाँ updateListofSoundsFile मेरी है कस्टम विधि कोड है:

-(void)updateListofSoundsFile 
{ 
    if(self.listOfSounds.count > 0) 
     [self.listOfSounds removeAllObjects]; 

    self.listOfSounds = [self.sql_ getAllDataFromAudioTable]; // get all record from DB 
    NSLog(@"%d",self.listOfSounds.count); 

    [self.tblView reloadData]; 
} 

कृपया कोई सुझाव दें, मैं इस मुद्दे को कैसे हल कर सकता हूं?
धन्यवाद :)

+0

अपना 'नंबरऑफरोइनससेक्शन' विधि पोस्ट करें। प्रश्न - आपकी 'commit एडिटिंग स्टाइल' विधि में, आप 'updateListOfSoundsFile' क्यों कहते हैं? आप पहले से ही एक ऑब्जेक्ट को हटाकर 'listOfSounds' को अपडेट कर सकते हैं। सरणी से ऑब्जेक्ट को हटाने के बाद, तालिका से एक पंक्ति को हटाने के लिए बस 'DeleteRowsAtIndexPaths' पर कॉल करें।यह भी सत्यापित करें कि 'deleteSoundFileAudioTableWhereMainID' को हटाने के लिए आपका कॉल वास्तव में डेटाबेस से एक पंक्ति को हटा रहा है। – rmaddy

उत्तर

5

आप सरणी से भी निकालें आइटम befor tableview से कच्चे हटाने और क्योंकि सरणी से नहीं बल्कि tableview आइटम को निकालने के इस लाइन का उपयोग कर डेटा को फिर से लोड करने की आवश्यकता है।

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"main_id"]]; /// delete record from DB 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array 

     [self updateListofSoundsFile]; /// Custom method 
    } 
} 
+4

यह समाधान नहीं होना चाहिए। 1 - आपको टेबल को अपडेट करने से पहले हमेशा डेटा स्रोत से डेटा को हटा देना चाहिए। 2 - यदि आप 'रीलोडडाटा' पर कॉल करने जा रहे हैं, तो पहले 'कॉलरएटएंड इंडेक्सपाथ' पर कॉल करने का कोई कारण नहीं है। यह समाधान काम कर सकता है लेकिन यह किसी अन्य मुद्दे को छुपा रहा है। – rmaddy

+2

तो कृपया हमें मार्गदर्शन करें कि उचित समाधान क्या है सर कृपया उचित समाधान के साथ उत्तर दें। धन्यवाद –

+2

@ rmaddy- महोदय, यदि आपके पास अन्य सर्वोत्तम समाधान है तो कृपया ज्ञान को बेहतर बनाने के लिए हमें मार्गदर्शन करें। धन्यवाद –

0
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"main_id"]]; /// delete record from DB 
     [self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array 
     [self updateListofSoundsFile]; /// Custom method 
    } 
    [self.tblView reloadData]; 
} 
+0

कृपया बताएं कि यह समस्या कैसे हल करता है? 'UpdateListofSoundsFile' विधि पहले से ही 'reloadData' को कॉल करती है। – rmaddy

+0

यदि आप स्थिति के बाहर अद्यतन लिस्टोफॉउंडफाइल डालते हैं ... – hitesh

2

मैं इस एक ही समस्या में पड़ गए। मुद्दा यह था कि मैंने सेल के ऑब्जेक्ट को हटा दिया लेकिन जब मैंनेकी reloadData विधि का उपयोग किया, तो मेरी canEditRowAtIndexPath विधि को कॉल नहीं किया जा रहा जिसके परिणामस्वरूप उन कक्षों को संपादित करने में सक्षम होना जिन्हें मैं संपादित नहीं करना चाहता था। सच्चा फिक्स deleteRowsAtIndexPaths विधि को कॉल नहीं कर रहा था, लेकिन [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; विधि।

असल में यहाँ क्या हो रहा था है:

जब मैं reloadData कहा जाता है: कच्चे डेटा tableView से हटाया जा रहा नहीं कर रहा था के रूप में नितिन ने कहा। इसलिए यह समाधान नहीं है।

जब मैंने deleteRowsAtIndexPaths कहा: आईओएस अंतर्निहित डेटा और कोशिकाओं की संख्या (क्योंकि मैंने पहले से ही अंतर्निहित वस्तु को हटा दिया था) के बीच एक विसंगति का पता लगाया है। परिणाम एक दुर्घटना थी जो समाधान भी नहीं है (जाहिर है)।

अब फिक्स के लिए!

जब मैंने reloadRowsAtIndexPaths: कहा था, तो तालिका ने केवल उस सेल को फिर से लोड करने के लिए दृश्य देखा और यह कच्चे डेटा से छुटकारा पा लिया। यह समाधान है।

बल्कि dataSource वस्तु को हटाने, तो सेल कि है अनिवार्य रूप से इस बिंदु (जो एक दुर्घटना का कारण बनता है) पर कुछ भी नहीं द्वारा समर्थित दूर करने के लिए कोशिश कर रहा से, बस dataSource वस्तु निकालें, तब फिर से लोड कि tableView

की indexPath

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [myDataSourceArray removeObjectAtIndex:indexPath.row]; 
     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
} 

यह पहली बार मैं अवशिष्ट कच्चे डेटा कि बस reloadData विधि को फोन करके नहीं हटाया जाता है के साथ इस मुद्दे में भाग है: यहाँ विधि का सामान्य प्रारूप है। यह निश्चित रूप से सबसे सुंदर समाधान है जिसे मैंने अब तक देखा है।

हैप्पी कोडिंग! आशा है कि ये आपकी मदद करेगा।

0

यह मेरे लिए आता है, मैं नीचे के रूप में विश्लेषण किया:

रूप Nitin Gohel's जवाब पर rmaddy द्वारा टिप्पणी की:

  1. तुम हमेशा तालिका अद्यतन करने से पहले डेटा स्रोत से डेटा निकाल देना चाहिए।

    मुझे लगता है कि यह आदर्श तरीका है।

  2. यदि आप reloadData पर कॉल करने जा रहे हैं, तो deleteRowsAtIndexPath पर कॉल करने के लिए कोई कारण नहीं है।

    यह भी सही दिखता है।

मैं अस्पष्टता का विश्लेषण किया है, अगर मैं reloadData अपने crashishing बारे में, लेकिन एक बार मैं deleteRowsAtIndexPath बारे में यह अच्छी तरह से काम किया।

आशा किसी को उसके कारणों आदि

0

के लिए इस मुद्दे पर जोर देना होगा 'removeObjectAtIndex: indexPath' कुछ समय लगता है और मैं अपने [self.tblView reloadData] जल्दी बुलाया जा रहा है संदेह है। मैं एक नमूना कोड की कोशिश की और [UITableView beginUpdates] और साथ सफलता मिली [UITableView endUpdates] आप भी दुर्घटना से बचने के सकता है, तो फिर आप पुनः लोड करने से पहले एक छोटे से देरी डाल या हटाने पंक्तियों प्रयास नहीं किया है यह, हालांकि

[tableTable beginUpdates]; 
[tableArray removeObjectAtIndex:indexPath.row]; 
[tableTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableTable endUpdates]; 
संबंधित मुद्दे