Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用DeleteRowsatindExpath后未更新indexPath_Objective C_Uitableview - Fatal编程技术网

Objective c 使用DeleteRowsatindExpath后未更新indexPath

Objective c 使用DeleteRowsatindExpath后未更新indexPath,objective-c,uitableview,Objective C,Uitableview,我创建了一个方法,当用户想要删除单元格时,该方法会更新我的UITableView -(void)removeMoreFriendsRow:(NSNotification *)notification { NSDictionary *d = [notification userInfo]; NSIndexPath *index = [d objectForKey:@"indexPath"]; [self.p_currentFriends removeObjectAtI

我创建了一个方法,当用户想要删除单元格时,该方法会更新我的UITableView

-(void)removeMoreFriendsRow:(NSNotification *)notification {
NSDictionary *d = [notification userInfo];
NSIndexPath *index = [d objectForKey:@"indexPath"];
    
[self.p_currentFriends removeObjectAtIndex:index.row];
    
[self.o_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationRight];
}
p_currentFriends是一个NSMutableArray,其中包含UITableView打印的所有对象

这种方法在我使用一次时效果很好,但在我多次使用时就失败了

似乎每次调用deleteRowsatindExpath时,indexPath都保持不变

比如说,

  • 在第一次调用时,用户点击第二个单元格indexPath=[0,1],并删除正确的单元格
  • 在第二次调用时,用户点击第三个单元格,indexPath=[0,3]而不是[0,2]
我发现使其工作的唯一方法是使用重载数据,但我希望在单元格删除上有一个动画


如何执行此操作?

您应该将DelterowStatIndexPath:和其他表编辑操作与和结合起来。这可能会解决您的问题。

您所做的基本上是正确的,但您需要以某种方式重新加载
表视图。如果您不想重新加载整个
表视图
,请使用它,它也会为您提供一个
动画
-

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionNum];
[self.o_table reloadSections:indexSet 
            withRowAnimation:UITableViewRowAnimationFade];
Swift 2.2

我找到的最佳解决方案是设置一个
NSTimer
,并在0.5秒后调用重新加载数据,这样就有足够的时间让动画完成。
选择器(“loadData”)
是一个包含
tableview.reloadData()

Swift 3.0


参考上述答案,以下是我的解决方案:

- (void)deleteRowAtIndex:(NSIndexPath *)indexPath{
    [self.dataSource.datas removeObjectAtIndex:indexPath.row];

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];
    //    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
    //    [self.tableView reloadSections:indexSet
    //                  withRowAnimation:UITableViewRowAnimationNone];
    //    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
    NSLog(@"INDEXPATH:%@",indexPath);
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self.tableView selector:@selector(reloadData) userInfo:nil repeats:NO];
}

reloadSections:
方法可以工作,但它不会为您提供删除行的正常动画,而是使用淡入淡出动画。在iOS 11中,有一种新方法可解决此问题:

  [tableView performBatchUpdates:^(
  {
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  }
             completion:^(BOOL finished)
  {
    if( finished){ 
      [tableView reloadData];
  }
  }];

使用以下代码,我能够正确地执行此操作,但我必须对iOS 11之前的版本和iOS 11中的版本分别进行管理

请参见以下示例:

if (@available(iOS 11.0, *)) { // indexPath is your needed indexPath

    [self->chatTableVC.messagesTable performBatchUpdates:^{
    [self->chatTableVC.messagesTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    } completion:^(BOOL finished) {
         [self->chatTableVC.messagesTable reloadData];
    }];

} else {

    [self->chatTableVC.messagesTable beginUpdates];
    [self->chatTableVC.messagesTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self->chatTableVC.messagesTable endUpdates];

}

p\u currentFriends
真的是字典还是数组?很抱歉,它不是NSDictionary,而是NSMutableArray。这是我尝试过的,但不起作用`[self.o_表开始更新];[self.o_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:index]with RowAnimation:UITableViewRowAnimationRight];[self.o_table endUpdates]``
  [tableView performBatchUpdates:^(
  {
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  }
             completion:^(BOOL finished)
  {
    if( finished){ 
      [tableView reloadData];
  }
  }];
if (@available(iOS 11.0, *)) { // indexPath is your needed indexPath

    [self->chatTableVC.messagesTable performBatchUpdates:^{
    [self->chatTableVC.messagesTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    } completion:^(BOOL finished) {
         [self->chatTableVC.messagesTable reloadData];
    }];

} else {

    [self->chatTableVC.messagesTable beginUpdates];
    [self->chatTableVC.messagesTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self->chatTableVC.messagesTable endUpdates];

}