【问题标题】:scrollToRowAtIndexPath not scrolling correctly to the bottom on iOS 8在 iOS 8 上,scrollToRowAtIndexPath 无法正确滚动到底部
【发布时间】:2016-07-27 11:25:04
【问题描述】:

这里有一个奇怪的问题。我想将表格视图滚动到第 1 节中第 0 行的底部。我正在使用以下代码:

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1), atScrollPosition: .Bottom, animated: true)

它似乎在 iOS 9.0 及更高版本的模拟器中完美运行。然而,在我的设备(iOS 8.1)和 8.3 的模拟器上,它完全相反。它滚动到单元格的顶部而不是底部!看起来很奇怪。有什么想法我可能在这里做错了吗?真的很感激任何指点!谢谢

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

根据this answer 的类似问题,这似乎是iOS 8 中的一个错误。解决方法是将滚动延迟一分钟(此代码也可以在链接的答案中找到,但它在Objective-C中)。请注意,我没有测试此代码,因为我没有安装 iOS 8 模拟器。

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1), atScrollPosition: .Bottom, animated: true)
} 

编辑:这是 Swift 3 中的答案:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true)
}

【讨论】:

  • 你拯救了我的一天!如果您没有部分...只需输入 0(在我的情况下)
【解决方案2】:

试试这个:

if self.tempArray.count == 0 {
 }
else{
   let indexPath = NSIndexPath(forRow: tempArray.count - 1, inSection: 0)
    self.tblChat.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
    }

【讨论】:

    【解决方案3】:

    我有同样的问题,当我将 UITableViewScrollPosition 选项从 UITableViewRowAnimationBottom 设置为 UITableViewRowAnimationNone 时,神奇的事情发生了,这对我来说很好,这是代码

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.list.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多