简体   繁体   English

将tableView滚动到底部以选择最后一行

[英]Scroll tableView to bottom for last row selection

I have a table view which loads itself on selection of date popover. 我有一个表格视图,它会在选择日期弹出框时进行加载。 I want the last tableview cell (which is not visible currently) to be selected and it should scroll to Bottom 我希望选择最后一个tableview单元格(当前不可见),它应该滚动到Bottom

I have written the following code 我写了下面的代码

- (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];

        }
    }
}

The tableview cell is selected but it doesn't scroll to bottom for the first time, however if I select a different date from the date popover, it scrolls to the bottom and the cell is selected. 选中了tableview单元格,但它第一次没有滚动到底部,但是,如果我从日期弹出框中选择了另一个日期,它将滚动到底部并选择了该单元格。

I have called the function " scrollTableToIndex " in viewDidAppear as well but it doesn't work . 我在viewDidAppear也调用了函数“ scrollTableToIndex ”,但是它不能工作。

Also if I using this 另外,如果我用这个

[self.tableView setContentOffset:CGPointMake(0, CGFLOATMAX) animated:YES];

instead of the below line of code 而不是下面的代码行

CGFloat height = self.agendaList.contentSize.height - self.tableView.bounds.size.height;
 [self.tableView setContentOffset:CGPointZero animated:YES];

If i do this then the code works for 1st time but second time, after I select the date from popover, the tableview hangs. 如果我这样做,那么代码将第一次工作,但是第二次,当我从弹出窗口中选择日期后,表视图将挂起。

I have referred this link 我已经提到了此链接

Please Help. 请帮忙。

That's the code I'm using for that: 那就是我正在使用的代码:

- (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];
    }
}

I keep this as one of my UITableView category methods, so "self" is just UITableView 我将此保留为UITableView类别方法之一,因此“ self”只是UITableView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM