有什么想法可以解决我的 UICollectionView 在滚动和旋转时崩溃的问题吗?
我正在使用以下方法分别进行滚动和旋转,并且每种方法似乎都可以正常工作。我刚刚注意到同时做这两件事时我会遇到这个崩溃。因此,当我旋转设备并且在 prepareLayout 中计算新的布局属性时,这似乎与以下事实有关,即连续滚动触发“invalidateLayoutWithContext(invalidContext)”(见下文)。
想法?有没有办法在轮换期间暂停(或忽略它们)任何滚动响应?
旋转方法 在 View Controller 的 viewWillLayoutSubviews 中,我使整个布局无效
self.cal.collectionViewLayout.invalidateLayout()
滚动方式 为了让我有一个“粘性”装饰 View (标题),我不会使整个布局无效,因为它会降低性能,但请执行以下操作。在布局类中,我覆盖了 shouldInvalidateLayoutForBoundsChange
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
let invalidContext : UICollectionViewLayoutInvalidationContext = self.invalidationContextForBoundsChange(newBounds)
// Keep Header Sticky
invalidContext.invalidateDecorationElementsOfKind(GCCalendarLayoutKind_Decorative1, atIndexPaths: [headerDecorativeIndexPath])
// Apply Invalidation
self.invalidateLayoutWithContext(invalidContext)
// Return normal super return (just in case of future IOS upgrades)
return super.shouldInvalidateLayoutForBoundsChange(newBounds)
}
请注意,我在这里使装饰 View (标题)无效,而崩溃的错误是关于我的补充 View 布局不同。
错误
2015-10-30 07:14:30.181 test3_collectionview[17086:3102132] * Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UICollectionViewData.m:408 2015-10-30 07:14:30.185 test3_collectionview[17086:3102132] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'layout attributes for supplementary item at index path ( {length = 2, path = 0 - 0}) changed from index path: ( {length = 2, path = 0 - 0}); element kind: (Decorative1); frame = (0 1085.5; 320 16); zIndex = 1; to index path: ( {length = 2, path = 0 - 0}); element kind: (Decorative1); frame = (0 853.5; 320 16); zIndex = 1; without invalidating the layout' *** First throw call stack:
最佳答案
我不确定这是否足够,但我会按照这些思路尝试一下( Collection View 是 ScrollView ):
import CoreGraphics
class myController: UIViewController, UIScrollViewDelegate {
var myScrollView = UIScrollView()
override func viewDidLoad() {
self.view.addSubview(myScrollView)
}
// This will capture rotation events
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.myScrollView.scrollEnabled = false
coordinator.notifyWhenInteractionEndsUsingBlock( {_ in self.myScrollView.scrollEnabled = true} )
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// This will not ignore scroll but perhaps can help keeping things "tidy" and "performant" during rotation. Not sure
if myScrollView.scrollEnabled == false {
let offset = scrollView.contentOffset
myScrollView.setContentOffset(offset, animated: false)
}
}
}
关于ios - 滚动旋转时 UICollectionView 崩溃(索引路径处补充项目的布局属性已更改但未失效..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33425111/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
点向量坐标矩阵的几何意义介绍旋转矩阵的几何含义之前,先介绍一下点向量坐标矩阵的几何含义点:在一维空间下就是一个标量,如同一条直线上,以任意某一个位置为0点,以一定的尺度间隔为1,2,3...,相反方向为-1,-2,-3...;如此就形成了一维坐标系,这时候任何一个点都可以用一个数值表示,如点p1=5,即即从原点出发沿着x轴正方向移动5个尺度;点p2=-3,负方向移动3个尺度; 在一维坐标系上过原点做垂直于一维坐标系的直线,则形成了二维坐标系,此时描述一个点需要两个数值来表示点p3=(3,2),即从原点出发沿着x轴正方向移动3个尺度,在此基础上沿着y轴正方向移动两个尺度的位置就是点p3。