問(wèn)題描述
我正在嘗試使用 UITableView
來(lái)模仿 iMessage 氣泡文本行為.為了始終滾動(dòng)到底部,我在 viewDidLoad
和 viewDidAppear
時(shí)使用了 scrollToRowAtIndexPath
.這是因?yàn)楫?dāng)調(diào)用 viewDidLoad
方法時(shí),表格還沒(méi)有完全加載,所以我需要在 viewDidAppear
中額外滾動(dòng).這段代碼成功了.但是,我想要的不是動(dòng)畫(huà)滾動(dòng)(將 animated
設(shè)置為 NO
并不能解決此問(wèn)題),我希望表格始終從底部顯示,而不是加載表,然后轉(zhuǎn)到最后一行.
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 調(diào)用,因?yàn)閺?viewDidAppear 中滾動(dòng)會(huì)使第一次調(diào)用變得多余.每次導(dǎo)航回視圖時(shí)都會(huì)調(diào)用 viewDidAppear,但視圖初始化時(shí)只會(huì)調(diào)用一次 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.
我同意之前關(guān)于向用戶隱藏滾動(dòng)而不是更改 UITableView 加載數(shù)據(jù)的方式的建議.我的建議是在 viewWillAppear 方法中使用 scrollToRowAtIndexPath 方法,并將動(dòng)畫(huà)設(shè)置為 NO.之后,如果您必須在表格對(duì)用戶可見(jiàn)時(shí)添加新行,請(qǐng)使用 insertRowsAtIndexPaths:withRowAnimation: 在表格視圖底部添加一行.請(qǐng)務(wù)必注意在數(shù)據(jù)模型的末尾添加數(shù)據(jù),以便當(dāng)用戶離開(kāi)并返回時(shí),他/她會(huì)回到相同的布局.
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.
希望這會(huì)有所幫助.
剛剛看到您不接受先前答案的原因,并認(rèn)為我會(huì)再詳細(xì)說(shuō)明一下.我提出的解決方案需要最少的努力,避免一次又一次地調(diào)用 reloadData ,從而避免一次又一次地調(diào)用 scrollToRowAtIndexPath 方法.您只需在 viewWillAppear 中調(diào)用一次 scrollToRowAtIndexPath 即可滾動(dòng)到表格視圖的底部(這樣做時(shí)向用戶隱藏轉(zhuǎn)換),您無(wú)需再次這樣做.
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.
這篇關(guān)于從底部加載 UITableView的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!