2011-12-28 4 views
0

그래서 비슷한 주제를 보았습니다.하지만 왜 아직도 충돌하는지 이해하지 못합니다 ....UITableView에서 개체 제거

"항목"이라는 배열로 항목을 읽습니다. 잠시 후 새로운 항목을 추가하고 이전 항목을 삭제하려고합니다.

그래서 나는 '항목'에 본질적으로 물건을 추가하고 일부 오래된 항목을 삭제하려고합니다. 다음 아래의 방법을 실행하고보기를 편집 할 때 죽습니다. 오류는 코드 블록 아래에 게시됩니다.

도움을

감사

-(void) removeOldEntries: (int) numOfEntries 
{ 

    NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init] ; 

// Remove the first number of entries in the table view. This number is specified by the numOfEntries 
for (int i = numOfEntries - 1; i >= 0; i = i -1) 
{ 
    [entries removeObjectAtIndex:i]; 
} 

// Build deleteIndexPaths 
for (int i = numOfEntries - 1; i >= 0; i = i - 1) 
{ 
    // Add objects to our index pathes array (of things we need to delete) and then remove the objects from feeds array 
    [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
} 

// Start the editing of the TableView 
[self.tableView beginUpdates]; 
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 
// End the editing of the table view 
[deleteIndexPaths removeAllObjects]; 
[deleteIndexPaths release]; 
} 

오류 메시지 :!..

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976 
2011-12-27 22:43:04.490 v1.0[999:6003] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00e3dbe9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f925c2 objc_exception_throw + 47 
    2 CoreFoundation      0x00df6628 +[NSException raise:format:arguments:] + 136 
    3 Foundation       0x000d847b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
    4 UIKit        0x0035aa0f -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8424 
    5 UIKit        0x0034a433 -[UITableView endUpdates] + 42 
    6 v1.0        0x000050e6 -[NewsTableViewController removeOldArticles:] + 408 
    7 v1.0        0x00004d16 -[NewsTableViewController pullAndParseData] + 696 
    8 CoreFoundation      0x00dae67d __invoking___ + 29 
    9 CoreFoundation      0x00dae551 -[NSInvocation invoke] + 145 
    10 Foundation       0x000ff555 -[NSInvocationOperation main] + 51 
    11 Foundation       0x0006dbd2 -[__NSOperationInternal start] + 747 
    12 Foundation       0x0006d826 ____startOperations_block_invoke_2 + 106 
    13 libSystem.B.dylib     0x96653a24 _dispatch_call_block_and_release + 16 
    14 libSystem.B.dylib     0x96645cf2 _dispatch_worker_thread2 + 228 
    15 libSystem.B.dylib     0x96645781 _pthread_wqthread + 390 
    16 libSystem.B.dylib     0x966455c6 start_wqthread + 30 
) 
terminate called after throwing an instance of 'NSException' 
+1

당신이 간단하게 할 수있는 추가 및 제거 항목을있는 tableView의 데이터 소스 내의 및 전화에 의한 [있는 tableView reloadData]; – samfisher

답변

2

당신은 이것을 불필요하게 복잡하게 만듭니다. 삭제시 약간의 불일치가 발생하여 오류가 발생합니다.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //return for section 0 
    return [entries count]; 
} 

에 있는지 확인하고 변경하려면 removeOldEntries:

-(void) removeOldEntries: (int) numOfEntries 
{ 

[entries removeObjectsInRange:NSMakeRange(0, numOfEntries-1)]; 
[self.tableView reloadData]; 
} 
+0

감사합니다. 나는 그것이해야하는 것보다 더 복잡한 방법으로 만드는 def이었다! – Brad

2

사용 [_tableView reloadData]이 당신의 인생을 단순화합니다

그러나 예외는 배열에 사용자가 생각하는 내용이 포함되어 있지 않음을 나타냅니다. 귀하의 배열을 넣어 NSLog 시도.

+0

감사합니다. 배열을 살펴본 후 내가 예상하지 못한 몇 가지 사항이 있음을 확인했습니다. – Brad

관련 문제