2010-08-16 16 views
5

मेरे पास UITableView है और मूल रूप से मैं कुछ ऐप सेटिंग्स में बना रहा हूं, और यदि पहले खंड का UISegmentedControl इंडेक्स 1 पर टॉगल किया गया है, तो मैं एक नया अनुभाग प्रदर्शित करना चाहता हूं, लेकिन अगर इंडेक्स 1 पहले सेट किया गया था और उपयोगकर्ता इंडेक्स का चयन करता है 0 तो मैं अनुभाग को हटाने की जरूरत 2.संपादन मोड में नहीं होने पर UITableView अनुभाग जोड़ना/हटाना?

ऐसा करने के लिए मैं इस कोड UISegmentedControl's valueChanged event

if (segmentControl.selectedSegmentIndex == 0) 
{ 
    self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) { 

     NSArray *indexSections = [NSArray arrayWithObjects: 
      [NSIndexPath indexPathForRow:0 inSection: 
       [sections indexOfObject:FT_AND_IN]], 
      [NSIndexPath indexPathForRow:0 inSection: 
       [sections indexOfObject:FRACTION_PRECISION]], nil]; 
     [sections removeObject:FT_AND_IN]; 
     [sections removeObject:FRACTION_PRECISION]; 
     [self.tableView deleteRowsAtIndexPaths:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else { 
    self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
    [sections insertObject:FRACTION_PRECISION atIndex:2]; 
    NSArray *indexSections = [NSArray arrayWithObjects: 
     [NSIndexPath indexPathForRow:0 inSection: 
      [sections indexOfObject:FT_AND_IN]], 
     [NSIndexPath indexPathForRow:0 inSection: 
      [sections indexOfObject:FRACTION_PRECISION]], nil]; 
    [self.tableView insertRowsAtIndexPaths:indexSections 
     withRowAnimation:UITableViewRowAnimationRight]; 
} 

पर सक्रिय होने NSMutableArraysections बुलाया कहाँ सभी वर्गों की सूची है सेट था। प्रत्येक खंड में केवल 1 पंक्ति होती है इसलिए कोई उप-सारणी की आवश्यकता नहीं होती है।

हालांकि जब हर हिस्से का मूल्यांकन मैं इस त्रुटि मिलती है:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], 
    /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:904 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
    reason: 'Invalid update: invalid number of sections. The number of sections 
    contained in the table view after the update (6) must be equal to the number of 
    sections contained in the table view before the update (4), plus or minus the 
    number of sections inserted or deleted (0 inserted, 0 deleted).' 

मैं और करने के लिए सत्यापित करने के बाद 4 वर्गों पहले कि यह था, इसे सही ढंग से sections सरणी के लिए दो वर्गों कहा, मैं यह बताया जोड़ा गया खंडों के लिए उचित सूचकांक। यह क्यों काम नहीं करता है?

मैंने लाइन को [self.tableView reloadData]; के साथ बदलने की कोशिश की और फिर यह ठीक काम करता है, लेकिन मैं उन वर्गों को जोड़ने/हटाने को एनिमेट करना चाहता हूं।

अद्यतन मैं इस सुझाव और जोड़ने का काम करता है की कोशिश की, लेकिन मैं

[self.tableView beginUpdates]; 
if (segmentControl.selectedSegmentIndex == 0) 
{ 
     self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && 
      [sections containsObject:FRACTION_PRECISION]) 
     { 

     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: 
       [sections indexOfObject:FT_AND_IN]] 
       withRowAnimation:UITableViewRowAnimationRight]; 
     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex: 
       [sections indexOfObject:FRACTION_PRECISION]] 
       withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else 
    { 
     self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
     [sections insertObject:FRACTION_PRECISION atIndex:2]; 
     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1]; 
    NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndex:2]; 
    [self.tableView insertSections:indexSet 
      withRowAnimation:UITableViewRowAnimationRight]; 
    [self.tableView insertSections:indexSet2 
      withRowAnimation:UITableViewRowAnimationRight]; 
} 
[self.tableView endUpdates]; 

मैं इस त्रुटि मिलती है को दूर करने पर दुर्घटनाग्रस्त हो रही है।

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** - 
    [NSIndexSet initWithIndexesInRange:]: Range {2147483647, 1} exceeds 
    maximum index value of NSNotFound - 1' 

FT_AND_IN और FRACTION_PRECISION वस्तुओं केवल इस कोड में डेटा स्टोर से निकाल जोड़ रहे हैं /, और वे बस const NSString वस्तुओं रहे हैं।

+0

उच्च स्कोर के साथ एक लोगों कोड – vodkhang

+0

यह ठीक काम करने के लिए माना जाता है स्वरूपित नहीं है देखने के लिए दु: खी होने के नाते। मुझे कोई समस्या नहीं दिख रही है। क्या आप इंडेक्स स्क्रिप्शन सरणी को दोबारा जांच सकते हैं? – vodkhang

उत्तर

4

वहां अपना अपरिवर्तित कोड पढ़ना बहुत मुश्किल है।

आप -[UITableView insertSections:withRowAnimation:] और -[UITableView deleteSections:withRowAnimation] पर विचार करना चाहते हैं, मुझे लगता है।

प्रयास करें:

if (segmentControl.selectedSegmentIndex == 0) 
{ 
    self.settings.useMetric = YES; 
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) { 

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]]; 
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]]; 

     [sections removeObject:FT_AND_IN]; 
     [sections removeObject:FRACTION_PRECISION]; 

     [self.tableView deleteSections:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 
else { 
    self.settings.useMetric = NO; 
    [sections insertObject:FT_AND_IN atIndex:1]; 
    [sections insertObject:FRACTION_PRECISION atIndex:2]; 

    NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]]; 
    [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]]; 

    [self.tableView insertSections:indexSections 
      withRowAnimation:UITableViewRowAnimationRight]; 
} 
+0

मुझे अभी भी यकीन नहीं है कि क्यों मायने का संपादित संस्करण काम नहीं करता है, लेकिन आपका करता है। आपके साथ एकमात्र समस्या '[self.tableView addSections ...]' पंक्ति 'insertSctions' होना चाहिए। धन्यवाद! – jamone

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