gpt4 book ai didi

ios - UITableView scrollToRowAtIndexPath 滚动到底部有时会使我的表格消失,为什么?

转载 作者:行者123 更新时间:2023-11-29 00:26:43 41 4
gpt4 key购买 nike

我希望我的表格 View 在第一次进入 View 时滚动到底部,但有时不会滚动到底部,有时会使我的表格消失。 (通常发生在第一次或第二次时)。

这是我的滚动代码:

-(void)tableScrollToBottom{

[self.tableView reloadData];

dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];

});
}

而且我在viewDidAppear中也有[self tableScrollToBottom],因为如果我只在viewDidLoad中滚动表格,表格总是不会滚动到最底部,我不知道为什么,这真的让我很困惑。

我尝试将[self.tableView layoutIfNeeded]放入[self tableScrollToBottom]中,并删除dispatch_async,似乎我第一次打开应用程序并进入 View 时仍然无法滚动到最底部,之后第一次,它会正确滚动到底部,我不知道第一次打开应用程序并进入该 View 和第一次之后有什么区别。(我的数据从服务器获取,并且来自之前的数据,并且我的表格 View 行高是动态的)。

现在这是我在 [self tableScrollToBottom] 中的代码:

-(void)tableScrollToBottom {

[self.tableView layoutIfNeeded];

NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

}

这是我调用 [self tableScrollToBottom] 的地方:

-(void)getDicData {

for (int i = 0; i < _csDictArr.count; i ++) {

[_chatPersonArray addObject:_csDictArr[i][@"Person"]];
[_chatDataArray addObject:_csDictArr[i][@"Message"]];
[_chatDateTimeArr addObject:_csDictArr[i][@"Time"]];

}

[self.tableView reloadData];
[self tableScrollToBottom];

}

我把[self getDicData]放在ViewDidLoad中,我也在viewDidAppear中调用tableScrollToBottom

-(void)viewDidAppear:(BOOL)animated {

[self tableScrollToBottom];

}

最佳答案

你应该使用 CATransaction 来确保 tableView 在滚动到底部之前完全重新加载。

[CATransaction begin];
[CATransaction setCompletionBlock:^{
CGFloat yOffset = 0;
if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
yOffset = self.tableView.contentSize.height - self.tableView.bounds.size.height;
}
[self.tableView setContentOffset:CGPointMake(0, yOffset) animated:YES];
}];
[self.tableView reloadData];
[CATransaction commit];

关于ios - UITableView scrollToRowAtIndexPath 滚动到底部有时会使我的表格消失,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42802239/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com