簡體   English   中英

iOS - UITableView 不滾動到底部

[英]iOS - UITableView not scrolling to bottom

我正在 iOS 中構建一個聊天應用程序,但在 UITableView 顯示所有消息時遇到了一些問題。 我希望表格視圖在加載時滾動到底部,以便用戶在打開應用程序時可以看到最新消息。

為此,我在刷新數據的函數中添加了此代碼:

    NSIndexPath* ipath = [NSIndexPath indexPathForRow: [self.messageOnTimeline numberOfRowsInSection:0]-1 inSection: 0];
    [self.messageOnTimeline scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES];

這工作正常,直到我向 tableview 添加標題。 我添加了以下代碼,現在加載應用程序時它不會滾動到底部:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
    return view;
}

有誰知道為什么?

非常感謝您的幫助

在您的viewDidload添加此代碼:

斯威夫特 5

let indexPath = IndexPath(item: <Row Index>, section: <Section Index>)
tableView.reloadRows(at: [indexPath], with: .automatic)

目標 C

NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:AddYourLastIndex inSection:0];
[self.tbl_presentation selectRowAtIndexPath:myIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];

試試這個代碼。它會滾動到 tableview 的底部

 [self.tableview setContentOffset:CGPointMake(0, self.tableview.contentSize.height-self.tableview.frame.size.height) animated:YES];

@Darshan Kunjadiya 答案是正確的。 即使我們可以在故事板中實現相同的效果。

選擇 tableView --> 從情節提要控件中單擊“添加缺少的約束”。 這會將約束添加到 tableView 和 View。

這幫助我解決了這個問題。 附上截圖以供參考。 截圖鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM