如何在底部插入带有动画的UITableViewCell? [英] How to insert a UITableViewCell at bottom with animation?

查看:202
本文介绍了如何在底部插入带有动画的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 {
        }
    }];
}

但是结果未正确显示:

它从屏幕底部开始滚动.

It started scroll from the bottom of the screen.

UITableViewUITableViewCell都使用了自动布局,并且UITableView已经在键盘顶部.

The UITableView and UITableViewCell are both used Auto Layout and the UITableView is on top of the keyboard already.

任何帮助将不胜感激.

推荐答案

尝试一下.

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

这篇关于如何在底部插入带有动画的UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆