效果參照下面截圖,來(lái)自映客直播APP

先分析一下需求,新評(píng)論是從下往上頂?shù)?,那我們收到新評(píng)論把它添加到tableView的最后一行。但這樣的話(huà),如果tableView所有的cell高度加起來(lái)還不滿(mǎn)一個(gè)tableView的高度,cell是從上往下填滿(mǎn)的。如下圖所示

我們需要的是下圖這樣,從一開(kāi)始就從下往上頂。

調(diào)整思路,我們可以把tableView倒過(guò)來(lái)(上下翻轉(zhuǎn)180°),每次插入數(shù)據(jù)都插在第0行。這樣就實(shí)現(xiàn)了每次插入數(shù)據(jù),都是從下往上頂。
_tableView.transform = CGAffineTransformMakeScale(1, -1);

但這樣,所有cell的內(nèi)容也是倒著的,那么再把contentView再倒轉(zhuǎn)一次,就正過(guò)來(lái)了。示例代碼如下
在自定義cell中:
self.contentView.transform = CGAffineTransformMakeScale(1, -1);
插入的代碼:
[self.modelArr insertObject:model atIndex:0];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationTop];
這樣就實(shí)現(xiàn)了。
效果圖

若想實(shí)現(xiàn)花椒那種用戶(hù)滑到最頂部的時(shí)候,再有新消息都不在往上滾動(dòng)了。則在insert的時(shí)候加個(gè)判斷,判斷是否到達(dá)tableView的底部(因?yàn)槲覀僼ableView翻轉(zhuǎn)過(guò),所以用戶(hù)看到的頂部其實(shí)是tableView的底部),判斷方法參考我的簡(jiǎn)書(shū) 如何判斷tableView滑動(dòng)到了底部 。如果你知道,可以跳過(guò)繼續(xù)往下。
效果圖:

首先你需要在tableView的底部加一個(gè)tableView的高度。
_tableView.contentInset = UIEdgeInsetsMake(0, 0, tableViewHeight, 0);
if (isReachBottom) ?//如果滑動(dòng)到達(dá)tableView底部
{
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationTop];
indexPath = [NSIndexPath indexPathForRow:self.commentDisplayedArr.count - 1 inSection:0];
//無(wú)動(dòng)畫(huà),滾動(dòng)到最后一個(gè)cell
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionNone animated:NO];
}