[英]How to scroll a UITableView to the tableFooterView
我有一個 UITableView,它的內容是動態變化的,就像一個 FIFO 堆棧。 單元格添加到底部並從頂部移除。
這很好用,我可以滾動到 indexPath,這樣最新的消息總是向下滾動到底部(就像聊天應用程序一樣)。
現在.. 我想在那個表格部分添加一個頁腳。 而不是使用
SrollToRowAtIndexPath
我希望能夠滾動到 tableFooterView。
任何我如何做到這一點的想法將不勝感激。
我正在使用它滾動到 tableView 的頁腳視圖:
[self.tableView scrollRectToVisible:[self.tableView convertRect:self.tableView.tableFooterView.bounds fromView:self.tableView.tableFooterView] animated:YES];
將UITableView
滾動到其 footerView 底部的最佳方法是簡單地設置內容偏移量。 您可以使用contentSize
和當前bounds
計算底部
這是我做的方式。
CGPoint newContentOffset = CGPointMake(0, [self.instanceOfATableView contentSize].height - self.instanceOfATableView.bounds.size.height);
[self.instanceOfATableView setContentOffset:newContentOffset animated:YES];
感謝 iphone_developer 這是我所做的:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
然后,當我添加行時,我調用它並且我的 tableView 的頁腳視圖一直可見
斯威夫特版本:
tableView.scrollToRow(at: IndexPath(row: self.tblview.numberOfRows(inSection: 0) - 1, section: 0), at: .top, animated: true)
這么多糟糕的答案。 :-)
最好的辦法:
[self.tableView scrollRectToVisible:
self.tableView.tableFooterView.frame animated:YES
];
也許是這樣的:
[tableView setContentOffset:CGPointMake(0, tableView.contentSize.height) animated:YES];
由於 UITableView 是 UIScrollView 的子類,您可以使用 UIScrollView 方法滾動到您喜歡的任何位置
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
只需設置 rect 以便在它可見時頁腳可見,您就會得到您的解決方案(您可以使用頁腳的 rect 或其他東西,只要您始終獲得正確的行為)。
我遲到的答案是給開發人員的,他們需要在顯示鍵盤時顯示頁腳。 正確的解決方案是考慮 contentInset 屬性(可以在顯示鍵盤后更改),所以非常簡單:
- (void)scrollToFooter {
UIEdgeInsets tableInsets = self.tableView.contentInset;
CGFloat tableHeight = self.tableView.frame.size.height - tableInsets.bottom - tableInsets.top;
CGFloat bottom = CGRectGetMaxY(self.tableView.tableFooterView.frame);
CGFloat offset = bottom - tableHeight;
if(offset > 0.f) {
[self.tableView setContentOffset:CGPointMake(0, offset) animated:YES];
}
}
我必須注意,在我的例子中,tableView 被添加到我自己的 ViewController 中,其中一個單元格具有 UITextField,它成為第一響應者。 要在顯示鍵盤時移動顯示頁腳,您需要注冊 keyboard did shown notification 並且(在 iOS7 上)在當前運行循環結束時執行此方法,因為在這種情況下 iOS7 會在我們的方法和頁腳不會顯示后自動執行 scrollToRowAtIndexPath。
-(void)registerKeyboardNotification {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShown:)
name:UIKeyboardDidShowNotification object:nil];
}
- (void)keyboardDidShown:(id)notification {
//move to the end of run loop
[self performSelector:@selector(scrollToFooter) withObject:nil afterDelay:.0];
}
最簡單的方法是在 LAST SECTION 的 LAST ROW 上使用 UITableViewScrollPositionTop。 這對我來說非常有效......
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:LAST_ROW inSection:LAST_SECTION] atScrollPosition:UITableViewScrollPositionTop animated:YES];
確保您的 Table Footer View 在底部間隔良好,並且它應該在視圖內很好地動畫......
希望這可以幫助...
這對我來說很好!
CGRect footerBounds = [addCommentContainer bounds];
CGRect footerRectInTable = [tableview convertRect:footerBounds fromView:addCommentContainer];
[tableview scrollRectToVisible:footerRectInTable animated:YES];
這在 Swift 4 中對我有用
func addRow() {
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: array.count - 1, section: 0)], with: .automatic)
tableView.endUpdates()
DispatchQueue.main.async {
self.scrollToBottom()
}
}
func scrollToBottom() {
let footerBounds = tableView.tableFooterView?.bounds
let footerRectInTable = tableView.convert(footerBounds!, from: tableView.tableFooterView!)
tableView.scrollRectToVisible(footerRectInTable, animated: true)
}
好主意,我自己也在找這個:)這是示例代碼,可以解決問題:
[tableView reloadData];
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
[tableView scrollToRowAtIndexPath:index
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
您的 tableView 頁腳中必須至少有一個單元格,問題是它將可見。 沒有時間測試,但我猜你能把它做得很小嗎?
此外,您必須在numberOfSectionsInTableView (至少一個用於表格,一個用於頁腳)、 numberOfRowsInSection (至少一個用於頁腳,您的最后一節)、 viewForHeaderInSection (除了您的單元格外為零)、 heightForHeaderInSection (也許如果您將其設置為零), cellForRowAtIndexPath (在頁腳中為您的單元格添加特殊情況)...
這應該足夠了。
這對我有用:
- (void)tableViewScrollToBottomAnimated:(BOOL)animated {
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
if (numberOfRows) {
if (self.tableView.tableFooterView) {
[self.tableView scrollRectToVisible:
self.tableView.tableFooterView.frame animated:YES
];
} else {
[self.tableView scrollToRowAtIndexPath:
[NSIndexPath indexPathForRow:numberOfRows-1 inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:animated
];
}
}
}
我正在使用這個:
- (void)scrollToBottom{
[self.myTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.fetchedResultsController.fetchedObjects count] -1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
我沒試過,但是當你滾動到最后一行+1 時會發生什么?
另一個想法是始終在末尾添加一個虛擬條目並使其具有不同的外觀,以便將其視為頁腳。 然后你可以隨時滾動到那個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.