【问题标题】:indexPath not updated after using deleteRowsAtIndexPaths使用 deleteRowsAtIndexPaths 后 indexPath 未更新
【发布时间】:2012-11-12 15:15:00
【问题描述】:

我创建了一个方法,当用户想要删除一个单元格时更新我的​​ 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 打印的所有对象。

这个方法我用一次就很好用,但是用多次就不行了。

看起来 indexPath 在每次调用 deleteRowsAtIndexPaths 时都保持不变。

例如,

  • 在第一次调用时,用户点击第二个单元格 indexPath = [0,1] 并删除正确的单元格,
  • 在第二次调用时,用户点击第三个单元格和 indexPath = [0,3] 而不是 [0,2]

我发现使它起作用的唯一方法是使用 reloadData 但我想在单元格删除时制作动画。

我该怎么做?

【问题讨论】:

  • p_currentFriends 真的是字典还是数组?
  • 抱歉,它不是 NSDictionary 而是 NSMutableArray。

标签: objective-c uitableview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    斯威夫特 2.2

    我找到的最佳解决方案是设置NSTimer 并在 0.5 秒后调用重新加载数据,这为动画的完成提供了足够的时间。 Selector("loadData") 是一个包含tableview.reloadData() 的方法

    _ = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("loadData"), userInfo: nil, repeats: false)
    

    Swift 3.0

    _ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(ViewController.loadData), userInfo: nil, repeats: false)
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        您应该用beginUpdates 和endUpdates 包围delteRowsAtIndexPaths: 和其他表格编辑操作。这可能会解决您的问题。

        【讨论】:

        • 这是我尝试过的,但它不起作用。 `[self.o_table beginUpdates]; [self.o_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationRight]; [self.o_table endUpdates];``
        【解决方案5】:

        参考上面的答案,这是我的解决方案:

        - (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];
        }
        

        【讨论】:

          【解决方案6】:

          使用以下代码,我可以正确执行此操作,但对于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];
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-27
            • 2018-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多