問題描述
我正在嘗試使用 UITableView
來模仿 iMessage 氣泡文本行為.為了始終滾動到底部,我在 viewDidLoad
和 viewDidAppear
時使用了 scrollToRowAtIndexPath
.這是因為當調用 viewDidLoad
方法時,表格還沒有完全加載,所以我需要在 viewDidAppear
中額外滾動.這段代碼成功了.但是,我想要的不是動畫滾動(將 animated
設置為 NO
并不能解決此問題),我希望表格始終從底部顯示,而不是加載表,然后轉到最后一行.
I'm trying to mimic the iMessage bubble text behaviour with an UITableView
. In order to always scroll to the bottom I'm using scrollToRowAtIndexPath
when viewDidLoad
and viewDidAppear
. This is because when the viewDidLoad
method is called, the table has not been completely loaded, so I need that extra scroll in viewDidAppear
. This code makes the trick. However, what I want is not an animated scroll (setting animated
to NO
does not solve this), I want the table to be displayed always from the bottom, not load the table and then go to the last row.
這可能嗎?我找不到任何完全符合所需行為的解決方案.
Is this possible? I can't find any solution that fits completely with the desired behaviour.
推薦答案
您可以避免從 viewDidLoad 調用,因為從 viewDidAppear 中滾動會使第一次調用變得多余.每次導航回視圖時都會調用 viewDidAppear,但視圖初始化時只會調用一次 viewDidLoad.
You can avoid the call from viewDidLoad because scrolling from within viewDidAppear makes that first call redundant. viewDidAppear is called every time you navigate back to the view but viewDidLoad is only called once when the view is initialized.
我同意之前關于向用戶隱藏滾動而不是更改 UITableView 加載數據的方式的建議.我的建議是在 viewWillAppear 方法中使用 scrollToRowAtIndexPath 方法,并將動畫設置為 NO.之后,如果您必須在表格對用戶可見時添加新行,請使用 insertRowsAtIndexPaths:withRowAnimation: 在表格視圖底部添加一行.請務必注意在數據模型的末尾添加數據,以便當用戶離開并返回時,他/她會回到相同的布局.
I would agree with earlier suggestions of hiding the scroll from the user instead of changing the way a UITableView is loading data. My suggestion would be to use the scrollToRowAtIndexPath method in the viewWillAppear method with animation set to NO. After that if you have to add a new row while the table is visible to the user, use insertRowsAtIndexPaths:withRowAnimation: to add a row at the bottom of the table view. Be sure to take care of adding the data at the end of your data model so that when the user navigates away and comes back, s/he comes back to the same layout.
希望這會有所幫助.
剛剛看到您不接受先前答案的原因,并認為我會再詳細說明一下.我提出的解決方案需要最少的努力,避免一次又一次地調用 reloadData ,從而避免一次又一次地調用 scrollToRowAtIndexPath 方法.您只需在 viewWillAppear 中調用一次 scrollToRowAtIndexPath 即可滾動到表格視圖的底部(這樣做時向用戶隱藏轉換),您無需再次這樣做.
edit: Just saw your reason for not accepting the previous answers and thought I'd elaborate a little more. The solution I propose would require minimum effort, avoid calling reloadData time and again and thus avoid calling the scrollToRowAtIndexPath method again and again. You only need to make one call to scrollToRowAtIndexPath in viewWillAppear to scroll to the bottom of the table view (hiding the transition from the user when doing so) and you wouldn't need to do that again.
這篇關于從底部加載 UITableView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!