Mik*_*ein 9 scroll uitableview ios swift
第二天我无法用表解决问题.
我们有一个segmentedControl,当更改时,会更改表格.假设控件的段中有3个元素,相应地,3个数组(重要的是,它们的大小不同)我需要在segmentedControl更改时向上滚动表.
似乎一切都很简单:contentOffset = .zero和reloadData()
但.这不起作用,我不知道为什么表不能向上滚动.
唯一有效的方法:
UIView.animate (withDuration: 0.1, animations: {
self.tableView.contentOffset = .zero
}) {(_) in
self.tableView.reloadData ()
}
Run Code Online (Sandbox Code Playgroud)
但是现在桌子出现时会出现另一个问题,可能会出现错误,因为segmentedControl已经改变了,而另一个数组中的数据可能没有,我们还没有完成reloadData()
也许我无法理解明显的事情)祝贺即将到来的假期!
Ash*_*iya 18
UItableView方法scrollToRow(at:at:animated :)滚动表格视图,直到索引路径标识的行位于屏幕上的特定位置.
使用
tableView.scroll(to: .top, animated: true)
Run Code Online (Sandbox Code Playgroud)
你可以使用我的扩展程序
extension UITableView {
public func reloadData(_ completion: @escaping ()->()) {
UIView.animate(withDuration: 0, animations: {
self.reloadData()
}, completion:{ _ in
completion()
})
}
func scroll(to: scrollsTo, animated: Bool) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
let numberOfSections = self.numberOfSections
let numberOfRows = self.numberOfRows(inSection: numberOfSections-1)
switch to{
case .top:
if numberOfRows > 0 {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: .top, animated: animated)
}
break
case .bottom:
if numberOfRows > 0 {
let indexPath = IndexPath(row: numberOfRows-1, section: (numberOfSections-1))
self.scrollToRow(at: indexPath, at: .bottom, animated: animated)
}
break
}
}
}
enum scrollsTo {
case top,bottom
}
}
Run Code Online (Sandbox Code Playgroud)
rba*_*win 10
在调用 reloadData 后尝试直接滚动到顶部后,我收到以下错误
[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0).'
Run Code Online (Sandbox Code Playgroud)
这为我修复了它:
tableView.reloadData()
if tableView.numberOfRows(inSection: 0) != 0 {
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
我找到了更好的方法来做到这一点。它就像一个魅力。
let topIndex = IndexPath(row: 0, section: 0)
tableView.scrollToRow(at: topIndex, at: .top, animated: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12133 次 |
| 最近记录: |