- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个包含 NSFetchedResultsController
的 UIViewController
。在将行插入到顶部之后,我想让这些行在插入之前保持可见。这意味着我需要进行一些计算以在更新后立即保留 contentOffSetY。计算是正确的,但我注意到 scrollViewDidScroll
在滚动到我指定的 contentOffsetY 后被调用,这导致状态损坏。这是日志记录:
Will apply an corrected Y value of: 207.27359771728516
Scrolled to: 207.5
Corrected to: 207.27359771728516
Scrolled to: 79.5 <-- Why is this logline here?
您可以直接克隆一个示例项目:https://github.com/Jasperav/FetchResultControllerGlitch (提交 https://github.com/Jasperav/FetchResultControllerGlitch/commit/d46054040139afeeb648e1e0b5b113bd98685b4a ,项目的最新版本只有小故障,对 scrollViewDidScroll 方法的奇怪调用现在消失了。如果你修复了小故障,我会奖励赏金。只需克隆最新版本,运行它并滚动一点点。你会看到奇怪的内容偏移(故障))。运行项目,你会看到奇怪的输出。这是 controllerDidChangeContent
方法:
public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
let currentSize = tableView.contentSize.height
UIView.performWithoutAnimation {
tableView.endUpdates()
let newSize = tableView.contentSize.height
let correctedY = tableView.contentOffset.y + newSize - currentSize
print("Will apply an corrected Y value of: \(correctedY)")
tableView.setContentOffset(CGPoint(x: 0,
y: correctedY),
animated: false)
print("Corrected to: \(correctedY)")
}
}
如果我在 tableView.endUpdates()
之后立即调用 tableView.layoutIfNeeded
,则委托(delegate)已被调用。调用委托(delegate)方法的原因是什么?有什么办法让它不滚动吗?
最佳答案
我下载了您的代码并进行了一些调整以解决故障问题。这是我所做的。
estimatedRowHeight
属性设置为某个数字 init() {
super.init(frame: .zero, style: .plain)
estimatedRowHeight = 50.0
register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
}
func reloadDataWithoutScroll() {
let lastScrollOffset = contentOffset
beginUpdates()
reloadData()
layoutIfNeeded()
endUpdates()
layer.removeAllAnimations()
setContentOffset(lastScrollOffset, animated: false)
}
controllerDidChangeContent
函数以使用 reloadDataWithoutScroll
函数 UIView.performWithoutAnimation {
tableView.performBatchUpdates({
tableView.insertRows(at: inserts, with: .automatic)
})
inserts.removeAll()
tableView.reloadDataWithoutScroll()
}
当我执行这些更改时,添加新行时它不会滚动。但是,当当前 contentOffset 为 0 时,它会滚动以显示新添加的行。从逻辑上讲,我认为这不是问题。
关于swift - scrollViewDidScroll 用奇怪的 contentOffsetY 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315281/
我认为在 iPhone 开发方面有不错的经验。据我所知..我确实设置了委托(delegate)..我从上到下都有UIView --> UIScrollView--> UITextView 我尝试了一切
我的 View Controller 底部有一个按钮。当用户向下滚动时,按钮必须附加到特定高度的 ScrollView 。 我需要在 contentOffset.y 达到特定值时立即将按钮附加到 Sc
我通过修改 PullToRefresh 库在 scrollViewDidScroll 中触发了 UIView 动画。我想要的是在用户开始拉动时触发动画,并在拉动以刷新标题 View 离开屏幕时终止它。
我正在编写程序以在滚动 UITableView 时移动名为“myView”的 UIView,请看下图: “myView”会随着tableview的contentoffset的变化而改变它的'y坐标',
我在 tgat 中有一个 UIScrollView 我有一个 UITableView。我已经禁用了 UITableView 的滚动所以我只想检查我的 ScrollView 是否已经到达底部,如果调用加
我已经设置了 scrollView 委托(delegate)并将逻辑实现到 scrollViewDidScroll 协议(protocol)方法中,如下所示,它永远不会进入第二种情况。我想知道我错过了
我正在使用 Collection View 来查看我的 rss 提要数据。我正在使用 -(void)scrollViewDidScroll:(UIScrollView *)scrollView 来检测
我正在使用 https://github.com/xmartlabs/XLPagerTabStrip 它有 containerview,这是一个 scrollview,子 viewController
我有两个类:Foo 和 Bar: // Foo protocol FooDelegate { func scrollViewDidScroll(offset: CGFloat) } class
当一个人在删除一行或添加一行时使用 UITableViewRowAnimation 时,有时如果该行位于 TableView 的极端,则表会滚动。 然而,即使它滚动,它似乎也没有在委托(delegat
大家好(和圣诞快乐) 希望有人能回答我的问题,那将是一个美好的圣诞节 礼物。 我的 UIViewController 一个表,在表部分区域中有一个分段控件。看起来像这样 每次用户单击分段控件时,我都会
我有这个委托(delegate),它打算在用户用手指滚动时执行: -(void)scrollViewDidScroll:(UIScrollView *)scrollView 我有一行代码滚动到开头:
出于某种原因,当我尝试创建一个函数 scrollviewdidscroll 时,它只给我一个 void 选项。我已经导入了 scrollviewdelegate,并在 viewdidload 中将委托
当用户使用 scrollViewDidScroll 滚动时,我正在尝试向我的(自定义)导航栏添加阴影,但它没有执行任何操作。我在另一个 View Controller 上有完全相同的代码,但它有一个
我创建了第一个 View Controller “MainViewController”,并将带有导出 contentContainer 的容器 View 放入其中。 我创建了第二个 View Con
我尝试在滚动屏幕时获取触摸位置。我尝试从 touchBegin() 获取它,但我做不到。那是因为 touchBegin 在滚动过程中从未调用过。我怎么解决这个问题???快速回答我。 最佳答案 使用 S
我的应用程序出现奇怪的结果。当我按下后退按钮时,它总是调用 scrollViewDidScroll 并且它使我的应用程序崩溃。 如果我没有滚动到底部(只需加载 View 然后按下后退按钮),就不会发生
所以人们对这个特定事件的大部分问题是在动画发生时让 scrollViewDidScroll 触发。我的情况正好相反。我觉得 scrollViewDidScroll 不应该在我的情况下触发。 让我进一步
我正在尝试检查用户是否在我的应用上上下滚动,但它没有调用我的 scrollViewDidScroll 方法 当我在应用程序上上下滚动时,为什么它不打印 received scroll 有什么想法吗?
我在 View 中有一个 Scrollview。我想使用函数 scrollViewDidScroll 来检查和更新箭头(左右)。 - (void)scrollViewDidScroll:(UIScro
我是一名优秀的程序员,十分优秀!