ios - 如何在底部插入带有动画的 UITableViewCell?

标签 ios uitableview autolayout ios-autolayout

我已阅读 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 {
        }
    }];
}

但是结果没有正确显示:

enter image description here

它从屏幕底部开始滚动。

UITableViewUITableViewCell 都使用了自动布局,而且 UITableView 已经在键盘上了。

任何帮助将不胜感激。

最佳答案

试试这个。

objective-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];
});

输出

enter image description here

swift

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

关于ios - 如何在底部插入带有动画的 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52148205/

相关文章:

ios - 根据方向更改 UITableViewCell 的内容

ios - 带自动布局的侧边菜单

ios - 在 Interface Builder 上展开一个 segue

ios - 在 Swift 中存储和使用庞大的字典

ios - 选中时可以在 UITableViewCell 上设置高度变化动画吗?

swift - 在 Swift 中添加表格单元格中的标签总和

ios - 使用自动布局使 UIView 和内容扩展以填充 UIToolbar 中的可用水平空间

ios - 属性文本 UILabel 在 UICollectionViewCell 中被截断,但在 UITableViewCell 中未被截断

iphone - 全景热点实现

ios - 我想将图像保存在 UIImageView swift 3 的特定文件夹中