从UITableView中删除对象

时间:2011-12-28 05:21:41

标签: iphone ipad 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'

2 个答案:

答案 0 :(得分:2)

使用[_tableView reloadData]。这将简化你的生活。

顺便说一下,你得到的异常表明你的数组没有包含你认为它应该包含的内容。尝试NSLog ging你的数组。

答案 1 :(得分: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];
}