首页 > 解决方案 > 如何在底部插入带有动画的 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在键盘顶部。

任何帮助将不胜感激。

标签: iosuitableviewautolayoutios-autolayout

解决方案


尝试这个。

目标 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)
}

推荐阅读