2011-09-04 8 views
5

के कारण TableView ऐप समाप्त हो गया है, मैं UITableViews और इसके साथ जाने वाली सभी चीज़ों का लटका पाने की कोशिश कर रहा हूं। फिलहाल मैं निम्नलिखित कोड है:'NSInternalInconsistencyException'

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 10; 
} 

-(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]; 
    } 

    [cell.textLabel setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]]; 

    return cell; 
} 

- (IBAction)killItem { 
    NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:2 inSection:0]; 
    [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationRight]; 
} 

और मैं निम्न त्रुटि जब "killItem" समारोह की शुरुआत मिलती है:

समाप्त एप्लिकेशन न आया हुआ अपवाद 'NSInternalInconsistencyException', कारण की वजह से: 'अमान्य अद्यतन: खंड 0 में पंक्तियों की अमान्य संख्या अद्यतन (10) के बाद किसी मौजूदा खंड में निहित पंक्तियों की संख्या अद्यतन (10) से पहले उस खंड में निहित पंक्तियों की संख्या के बराबर होनी चाहिए, प्लस या घटाएं पंक्तियों को उस अनुभाग से डाला या हटा दिया गया है (0 डाला गया, 1 हटाया गया)। '

जिस तरह से मैं इसे समझता हूं तालिका मूल रूप से एक प्रतिनिधि और डेटा स्रोत होता है जहां डेटा स्रोत, अन्य चीजों के साथ, यह निर्धारित करता है कि तालिका में कितनी पंक्तियां होनी चाहिए। यहां कुछ खोजों के माध्यम से स्टैक ओवरफ्लो में मैंने पाया है कि यह त्रुटि तब होती है जब "डेटा स्रोत वास्तविकता से मेल नहीं खाता", जब यह मौजूद पंक्तियों की खोज कर रहा है, जो मैंने हटा दिया है।

मुझे यह गलत मिल गया होगा लेकिन मुझे लगता है कि यह ऐसा कर रहा है। तो मेरा सवाल यह है कि, मैं इन्हें मिलान करने के लिए कैसे प्राप्त करूं ताकि मैं इस त्रुटि से बच सकूं?

संदर्भ के लिए, मैं में निम्नलिखित पदों के लिए समझने मैं क्या करने की जरूरत के बिना देखा है:

Error : Number of Rows In Section in UITableView in iPhone SDK

Slide UITableViewCell

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

killItem में [tbl reloadData], [tbl beginUpdate] ... [tbl endUpdate] जोड़ना समारोह, मेरी समस्या सेनानी मदद करने के लिए प्रतीत नहीं होता है।

अग्रिम धन्यवाद, टोबियास Tovedal

उत्तर

12

यह बहुत आसान है, समस्या यहाँ निहित है:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 10; 
} 

प्रतिनिधि सेब द्वारा इस्तेमाल किया पैटर्न, मतलब है कि आप कर रहे हैं एक अपने प्रतिनिधियों के माध्यम से UITableView की सामग्री के प्रबंधन, जिसका अर्थ है पर जिम्मेदार है कि, अगर आप एक पंक्ति हटाते हैं, आप डेटा मॉडल से डेटा हटाने का भी जिम्मेदार हैं।

तो, पंक्ति को हटाने के बाद, यह समझ में आता है कि अनुभाग में पंक्तियों की संख्या "9" तक घट जाएगी, फिर भी, आपका कार्य हमेशा 10 लौट रहा है, और इस प्रकार अपवाद फेंक रहा है।

आमतौर पर, जब एक तालिका का उपयोग कर, और सामग्री बदल जाएगा, एक NSMutableArray बहुत आम है, आप कुछ इस तरह करते हैं:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [arrayWithStuff count]; 
} 

और फिर, सरणी से एक वस्तु (removeObjectAtIndex:) को हटाने को स्वचालित रूप से होगा पंक्तियों की संख्या अद्यतन करें।

:

+0

आपको बहुत बहुत धन्यवाद, जिसने मेरी समस्या को अच्छी तरह से हल किया! : डी –

12

टोबीस, आप क्या करने की जरूरत है जब पंक्तियों को हटाने

// tell the table view you're going to make an update 
[tableView beginUpdates]; 

// update the data object that is supplying data for this table 
// (the object used by tableView:numberOfRowsInSection:) 
[dataArray removeObjectAtIndex:indexPath.row]; 

// tell the table view to delete the row 
[tableView deleteRowsAtIndexPaths:indexPath 
      withRowAnimation:UITableViewRowAnimationRight]; 

// tell the table view that you're done 
[tableView endUpdates]; 


है जब आप endUpdate फोन नंबर tableView:numberOfRowsInSection: से लौटे के समान होना चाहिए beginUpdate पर संख्या पंक्तियों की संख्या से कम हो गई है।

+0

धन्यवाद माइक रूप में अच्छी तरह (संपादित लगभग उसी समय उत्तर के रूप में माइक सूखी घास, क्योंकि ऐसा लगता है आप पहले से ही इसके बारे में पढ़ा है, उनकी सलाह भी निम्न प्रयास मैं छोड़ दिया शुरू/अंत अद्यतन!)! :) –

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