2012-02-08 3 views
0

내 화면 중 하나에 바 버튼 항목이 있습니다. 버튼을 누르면 내 ViewController에서 addItem 액션이 트리거됩니다.iOS ViewController 액션이 예외를 일으키는 이유는 무엇입니까?

- (IBAction)addItem 
{ 

    // items is an Array storing list items 
    int newRowIndex = [items count]; 

    ChecklistItem *item = [[ChecklistItem alloc] init]; 
    item.text = @"I am a new row"; 
    item.checked = NO; 
    [items addObject:item]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0]; 
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

내가 버튼을 누르면 응용 프로그램이 종료하고이 예외가 발생합니다 :이 예외가 발생하는 이유

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011) 
Copyright 2004 Free Software Foundation, Inc. 
GDB is free software, covered by the GNU General Public License, and you are 
welcome to change it and/or distribute copies of it under certain conditions. 
Type "show copying" to see the conditions. 
There is absolutely no warranty for GDB. Type "show warranty" for details. 
This GDB was configured as "x86_64-apple-darwin".Attaching to process 1799. 
2012-02-08 16:04:08.146 Checklists[1799:f803] *** Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableViewSupport.m:386 
2012-02-08 16:04:08.149 Checklists[1799:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.' 
*** First throw call stack: 
(0x13bb052 0x154cd0a 0x1363a78 0x99b2db 0x26eb49 0x27c0e3 0x95e3a 0xa182a 0xa1869 0x2e7d 0x13bcec9 0x155c2 0x250d54 0x13bcec9 0x155c2 0x1555a 0xbab76 0xbb03f 0xba2fe 0x3aa30 0x3ac56 0x21384 0x14aa9 0x12a5fa9 0x138f1c5 0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x12a4879 0x12a493e 0x12a9b 0x1ec8 0x1e25) 
terminate called throwing an exceptionsharedlibrary apply-load-rules all 
Current language: auto; currently objective-c 

사람이 설명 할 수 있습니까?

답변

1

새로운 행을 삽입하기 전에, 당신이 업데이트에 대한 테이블을 준비해야합니다 또한

[[self tableview] beginUpdates]; 

[[self tableview] insertRowsAtIndexPaths ... 

[[self tableview] endUpdates]; 

를, 그것은 중요하다 당신 :

-(void)tableView:(UITableView *)tableview numberOfRowsInSection 

반환 가장 최대 - 투 - 전화를 걸 때의 날짜 행 수 :

[tableview endUpdates]; 

numberOfRows 메소드 만 retu 귀하의 항목 배열에 개체의 개수를 rns, 당신은 괜찮을 것입니다 ...

+0

아 - 나는 tableView : numberOfRowsInSection 항상 5를 반환하기 위해 밖으로 stubbed 것 : '[items count] return'을 변경하면 발행물. – bodacious

+0

또한이 경우에는 [[[self tableview] beginUpdates];와 [[self tableview] endUpdates];가 필요하지는 않습니다. - 도와 주셔서 정말로 고맙습니다 – bodacious

관련 문제