如何在动画的底部插入UITableViewCell?

我已阅读https://stackoverflow.com/questions/

并试着这样:

//UITableView self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 50; //Update the full code for the "return" key action - (void)inputViewDidTapReturn:(MyInputView *)inputView text:(NSString *)text{ NSLog(@"send text-----%@",text); [self.messageManager sendMessageTo:self.sessionID message:text completion:^(BOOL success, Message *message) { if (success) { [self.dataArray addObject:message]; NSIndexPath * bottomIndexPath = [NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:@[bottomIndexPath] withRowAnimation:UITableViewRowAnimationLeft]; [self.tableView endUpdates]; [self.tableView scrollToRowAtIndexPath:bottomIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } else { } }]; } 

但结果没有正确显示:

在此处输入图像描述

它从屏幕底部开始滚动。

UITableViewUITableViewCell都使用自动布局, UITableView已经在键盘顶部。

任何帮助将不胜感激。

尝试这个。

目标C.

 [self.dataArray addObject:message]; [self.tableView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ NSIndexPath *bottomIndexPath = [NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0]; [self.tableView scrollToRowAtIndexPath:bottomIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; }); 

产量

在此处输入图像描述

迅速

 self.dataArray.add(messasge) self.tableView.reloadData() DispatchQueue.main.async { let bottomIndexPath = IndexPath(row: self.dataArray.count-1, section: 0) self.tableView.scrollToRow(at: bottomIndexPath, at: .bottom, animated: true) } 

正确的方法是使用beginUpdatesendUpdates方法将单元格插入表视图。 首先,您应该将项目添加到数组中。

array.append(item)

这不会更新表视图,只更新数组,将单元格添加到刚刚调用的视图中

 tableView.beginUpdates() tableView.insertRows(at: [IndexPath(row: array.count-1, section: 0)], with: .automatic) tableView.endUpdates()