无法删除行 - UITableView iOS 7



我正在尝试清除UITableView。首先,我尝试清除[self.tableView reloadData]; listOfPosts = nil但 tableView 仍然显示 10 行。然后我尝试逐行删除,但结果是一样的。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Number of sections: %i", [self.tableView numberOfSections]); // => 1
NSLog(@"Number of rows in section 0: %i", [self.tableView numberOfRowsInSection:0]); // => 10
[self.tableView beginUpdates];
NSMutableArray *rowsToDelete = [[NSMutableArray alloc] init];
int nPosts = [self.tableView numberOfRowsInSection:0];
for (int i = 0; i < nPosts; i++) {
    [rowsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
currentPostList = nil;
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:rowsToDelete] withRowAnimation:UITableViewRowAnimationNone];                         
[self.tableView endUpdates];
[self.tableView reloadData];
NSLog(@"Rows after endUpdates: %i", [self.tableView numberOfRowsInSection:0]); // => 0
NSLog(@"Visible rows: %i", [[self.tableView visibleCells] count]); // => 0
// But the tableView still show 10 rows
}];

PD:我正在使用UITableViewController,所以self.tableView是自动生成的

(对不起,我的英语不好)

您是否

尝试更改进入该方法的值:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

这将改变表视图中的行数。重新加载表不会执行任何操作,除非模型更改了此方法声明的值。

最新更新