2009-06-16 4 views
13

इस समस्या ने मुझे पिछले घंटों तक व्यस्त रखा है। मेरे पास प्रत्येक खंड में एक पंक्ति के साथ दो खंड हैं। जब मैं किसी एक वर्ग में पंक्ति को हटा देता हूं, तो यह एक अपवाद फेंकता है कि यह एक अमान्य अद्यतन है (अद्यतन से पहले और बाद में पंक्तियों/खंडों की संख्या समान नहीं है)। यह समझ में आता है क्योंकि मैं एक खंड की अंतिम पंक्ति को हटाता हूं और इसलिए मैं खंड को हटा देता हूं। सवाल यह है कि अपवाद से कैसे बचें।किसी अनुभाग की अंतिम पंक्ति को कैसे हटाएं?

सब कुछ मेरे डेटा स्रोत के साथ ठीक है। मैंने चेक किया और पुनः जांच की (मुझे विश्वास है)।

तो, थ्रेड राज्यों के शीर्षक के रूप में, आप अपवाद प्राप्त किए बिना किसी अनुभाग की अंतिम पंक्ति को कैसे हटाते हैं?

धन्यवाद,

बार्ट

+0

"सूत्र के शीर्षक" यह एक प्रश्न और उत्तर साइट है, एक मंच नहीं आपका मतलब है प्रश्न का शीर्षक। –

उत्तर

26

जब आप एक पंक्ति हटा देते हैं और इस पंक्ति अपने अनुभाग के अंतिम एक है, आप भी हिस्से को हटाना की जरूरत है। असल में, आपको उन इंडेक्सपैथ्स को ट्रैक करना होगा जिन्हें आप हटाना चाहते हैं, जो पंक्तियों से जुड़े होते हैं, और उन अनुभागों से संबंधित इंडेक्स जिन्हें हटाया जाना आवश्यक है क्योंकि उनमें अब पंक्तियां नहीं हैं। आप इसे कर सकता है इस प्रकार है:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; 

हर बार जब आप tableView के किसी विशिष्ट अनुभाग से संबंधित अपने मॉडल सरणी से एक वस्तु को हटाते हैं, तो देखें कि क्या सरणी गिनती शून्य है, जो मामले में एक सूचकांक अनुभाग का प्रतिनिधित्व जोड़ने अनुक्रमित करने के लिए:

[array removeObjectAtIndex:indexPath.row]; 
if(![array count]) 
    [indexes addIndex: indexPath.section]; 

पंक्तियों से संबंधित indexPaths के सभी निर्धारित हटाए जाने के लिए है, तो tableView अद्यतन इस प्रकार है:

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 

यह मैं और ओ के लिए काम किया मैं लोगों ने दृष्टिकोण का सुझाव दिया।

+0

यह विकल्प काम करता है हालांकि जब आप अंतिम खंड को हटाते हैं तो दूसरा अपवाद फेंक दिया जाता है (लेकिन मैं इसे हल करने का एक तरीका समझूंगा)। मुझे विश्वास करना मुश्किल लगता है कि यह एकमात्र तरीका है। ऐसा लगता है कि इतना अनौपचारिक/अनौपचारिक है। यह सिर्फ काम नहीं करता ;-)। हालांकि आपके इनपुट के लिए धन्यवाद। –

+2

इसमें जोड़ने के लिए, जब मैं किसी सेक्शन में अंतिम पंक्ति को हटा रहा हूं, तो मैं केवल डिलीटक्शन कॉल करता हूं: RowAnimation:। मैं deleteRowsAtIndexPaths को कॉल नहीं करता हूं: RowAnimation:। एनीमेशन एक जैसा दिखता है, और यह कोई अपवाद नहीं फेंकता है। – Alex

+0

आप पिछले खंड की अंतिम पंक्ति को हटाने के साथ कैसे निपटते हैं? –

1

शायद यह आप के लिए काम कर सकते हैं:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    NSString *key = [self.keys objectAtIndex:section]; 
    NSMutableArray *rowsInSection = [self.dict objectForKey:key]; 
    [rowsInSection removeObjectAtIndex:row]; 

    NSUInteger rowsInSectionCount = [rowsInSection count]; 

    if (rowsInSectionCount > 0) { 
     // If we still have rows in this section just delete the row 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else { 
     // There are no more rows in this section 
     [self.dict removeObjectForKey:key]; 
     [self.keys removeObjectAtIndex:section]; 

     NSUInteger sectionCount = [self.keys count]; 

     if (sectionCount > 0) { 
      // If we still have 1 or more sections left just delete this section 
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     else { 
      // If there are no more rows and sections left just delete the last row 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView reloadData]; 
     } 
    } 
} 

आशा यह आपके लिए क्या देख रहे थे है। अपने मॉडल में डेटा हटाने के बाद

// Either delete some rows within a section (leaving at least one) or the entire section. 
    if ([indexPath section] < 0) { 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if ([indexPath section] == 0) { 
     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
1

यह एक चाल करना चाहिए

tableView.beginUpdates()      

    let indexSet = NSMutableIndexSet() 
    indexSet.addIndex(indexPath.section) 
    tableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade) 

    tableView.endUpdates() 

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

  • कोई संबंधित समस्या नहीं^_^