【问题标题】:Scroll to bottom in UITableView在 UITableView 中滚动到底部
【发布时间】:2016-07-22 02:47:46
【问题描述】:

我有一个使用UITableView 构建的聊天屏幕。我想在从其他视图控制器打开屏幕的那一刻滚动到UITableView 的底部。但是使用简单的滚动到底部功能,如果聊天时间很长,会显示一个混蛋。有其他选择吗?

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    dispatch_async(dispatch_get_main_queue(), ^{

        NSIndexPath* lastIndexPath = [NSIndexPath indexPathForRow:_messagesArray.count-1  inSection:0];
        [_tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    });
}

【问题讨论】:

  • 改用 setContentOffset

标签: ios objective-c uitableview uiscrollview


【解决方案1】:

使用UITableViewScrollPositionBottom 代替UITableViewScrollPositionTop

【讨论】:

    【解决方案2】:
    - (void)viewWillAppear:(BOOL)animated {
    
        [super viewWillAppear:animated];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat height = _tableView.contentSize.height - _tableView.bounds.size.height;
            [_tableView setContentOffset:CGPointMake(0, height) animated:YES];  
        });
    }
    

    【讨论】:

      【解决方案3】:

      我想这就是你想要的:

      extension UITableView {
      
          func scrollToBottom() {
              scrollToRow(at: IndexPath(row: numberOfRows(inSection: numberOfSections-1)-1, section: numberOfSections-1),
                          at: .bottom, animated: true)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-27
        • 2013-09-12
        • 2012-01-07
        • 1970-01-01
        • 2019-06-16
        • 2013-05-05
        相关资源
        最近更新 更多