将 tableView 滚动到底部以选择最后一行
Scroll tableView to bottom for last row selection
我有一个 table 视图,它在日期弹出窗口的 selection 上自行加载。
我希望最后一个 table 视图单元格(当前不可见)被 selected 并且它应该滚动到底部
我写了下面的代码
- (void)viewDidLoad {
[_tableView reloadData];
[self scrollTableToIndex];
[_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedMeetingIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:_tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedMeetingIndex inSection:0]];
}
-(void)scrollTableToIndex{
if(_selectedMeetingIndex <= _arrMeetingsList.count / 2){
_isTableBottomScrolled = NO;
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedMeetingIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}else{
NSLog(@"_tableView.contentOffset.y %f",_tableView.contentOffset.y);
if (!_isTableBottomScrolled) {
_isTableBottomScrolled = YES;
CGFloat height = self.agendaList.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointMake(0, height) animated:YES];
}
}
}
table视图单元格已 selected 但它第一次没有滚动到底部,但是如果我 select 与日期弹出窗口不同的日期,它滚动到底部,单元格为 selected.
我也在 viewDidAppear
中调用了函数“scrollTableToIndex
”,但它不起作用。
如果我使用这个
[self.tableView setContentOffset:CGPointMake(0, CGFLOATMAX) animated:YES];
而不是下面的代码行
CGFloat height = self.agendaList.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointZero animated:YES];
如果我这样做,那么代码第一次工作但第二次工作,在我 select 来自弹出窗口的日期之后,table 视图挂起。
我已经提到了这个link
请帮忙。
这就是我为此使用的代码:
- (void) scrollToBottomAnimated:(BOOL) animated
{
CGSize contentSize = self.contentSize;
CGSize boundsSize = self.bounds.size;
if (contentSize.height > boundsSize.height - self.contentInset.bottom)
{
CGPoint bottomOffset = CGPointMake(0, contentSize.height - boundsSize.height + self.contentInset.bottom);
[self setContentOffset:bottomOffset animated:animated];
}
}
我将其保留为我的 UITableView 类别方法之一,因此 "self" 只是 UITableView
我有一个 table 视图,它在日期弹出窗口的 selection 上自行加载。 我希望最后一个 table 视图单元格(当前不可见)被 selected 并且它应该滚动到底部
我写了下面的代码
- (void)viewDidLoad {
[_tableView reloadData];
[self scrollTableToIndex];
[_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedMeetingIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:_tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedMeetingIndex inSection:0]];
}
-(void)scrollTableToIndex{
if(_selectedMeetingIndex <= _arrMeetingsList.count / 2){
_isTableBottomScrolled = NO;
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedMeetingIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}else{
NSLog(@"_tableView.contentOffset.y %f",_tableView.contentOffset.y);
if (!_isTableBottomScrolled) {
_isTableBottomScrolled = YES;
CGFloat height = self.agendaList.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointMake(0, height) animated:YES];
}
}
}
table视图单元格已 selected 但它第一次没有滚动到底部,但是如果我 select 与日期弹出窗口不同的日期,它滚动到底部,单元格为 selected.
我也在 viewDidAppear
中调用了函数“scrollTableToIndex
”,但它不起作用。
如果我使用这个
[self.tableView setContentOffset:CGPointMake(0, CGFLOATMAX) animated:YES];
而不是下面的代码行
CGFloat height = self.agendaList.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointZero animated:YES];
如果我这样做,那么代码第一次工作但第二次工作,在我 select 来自弹出窗口的日期之后,table 视图挂起。
我已经提到了这个link
请帮忙。
这就是我为此使用的代码:
- (void) scrollToBottomAnimated:(BOOL) animated
{
CGSize contentSize = self.contentSize;
CGSize boundsSize = self.bounds.size;
if (contentSize.height > boundsSize.height - self.contentInset.bottom)
{
CGPoint bottomOffset = CGPointMake(0, contentSize.height - boundsSize.height + self.contentInset.bottom);
[self setContentOffset:bottomOffset animated:animated];
}
}
我将其保留为我的 UITableView 类别方法之一,因此 "self" 只是 UITableView