删除多个单元格后,uitableview添加了一些行
问题描述:
我正在使用“滑动删除”,如果要删除序列中的多个单元格,则uitableview中的某些单元格会加倍或者某些已删除的单元格出现。因为我不能发表任何图片,我将描述的行为表:删除多个单元格后,uitableview添加了一些行
- 表删除单元前:用户1,用户2,用户3,用户4,用户5,添加新的用户。
- 删除用户1和用户5后的表格:用户2,用户3,用户4,添加新用户,添加新用户(但不可选)。
- 删除用户3后的表格:用户4,用户2,添加新用户,用户5(可选),添加新用户(不可选)。
方法,其中我删除单元格的样子:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.usersTable beginUpdates];
UITableViewCell *cell = (UITableViewCell *)[self.usersTable cellForRowAtIndexPath:indexPath];
NSString *userNameToDelete = cell.textLabel.text;
// Data source
[self.appDict removeObjectForKey:userNameToDelete];
self.arrayOfUserNames = [[NSMutableArray alloc] initWithArray:[self.appDict allKeys]];
[self.appDict writeToFile:self.pathOfAppFile atomically:YES];
// Deleting cell
[self.usersTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.usersTable endUpdates];
}
}
法支持编辑:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self.arrayOfUserNames count]) {
return NO;
}
else {
return YES;
}
在节中的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arrayOfUserNames count] + 1;
}
我已经尝试[self.usersTable重载],但一切都停留在s AME。我也尝试在numberOfRowsInSection中更改表的大小:但这也无济于事。任何想法我做错了什么?
答
haven't你实现
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//delete your user record
}
}
?
如果不是,Tableview将删除单元格,但底层数据将不会被修改,并导致这样一个奇怪的行为。
编辑:
尝试使用:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *userNameToDelete = [[self arrayOfUserNames] objectAtIndex:indexPath.row];
// Data source
[self.appDict removeObjectForKey:userNameToDelete];
self.arrayOfUserNames = [[NSMutableArray alloc] initWithArray:[self.appDict allKeys]];
[self.appDict writeToFile:self.pathOfAppFile atomically:YES];
[tableView reloadData];
}
}
给一个简短的说明:
时
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
被调用时,TableCell的是从已删除tableview,所以你不必处理这个。如果你这样做,你会删除下一个单元格。
我将self.usersTable更改为该方法中的tableView,但行为保持不变。你有这个想法吗? – user1165156 2013-05-07 13:09:52
哦对不起。似乎我有点失明;) – 2013-05-07 17:14:28
你不必手动删除表格。我会编辑我的答案... – 2013-05-07 17:36:35