我有一个 UICollectionView,它使用 UICollectionViewFlowLayout 的自定义子类,使节标题在滚动时粘在屏幕顶部,就像 UITableView 的普通样式一样。我的代码基于这种方法:
http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using
此外, Collection View 设置为在用户滚动到底部时加载更多结果。也就是说,当用户到达 Collection View 的底部时,网络请求会加载更多数据,然后重新加载 Collection View ,即调用 reloadData。
它似乎运行良好,除了我通过运行 iOS 7 和 7.1 的 TestFlight 从 beta 测试人员那里收到了一些崩溃报告,他们说当他们向下滚动到底部时偶尔会发生以下情况(即触发更多结果加载)很快:
*** -[__NSArrayM objectAtIndex:]: index 28 beyond bounds [0 .. 16]
PRIMARY THREAD THREAD 0
__exceptionPreprocess
objc_exception_throw
-[__NSArrayM objectAtIndex:]
-[UICollectionViewFlowLayout(Internal) _frameForItemAtSection:andRow:usingData:]
-[UICollectionViewFlowLayout layoutAttributesForItemAtIndexPath:usingData:]
-[UICollectionViewFlowLayout layoutAttributesForItemAtIndexPath:]
-[MyCustomCollectionViewFlowLayout layoutAttributesForItemAtIndexPath:]
-[MyCustomCollectionViewFlowLayout layoutAttributesForSupplementaryViewOfKind:atIndexPath:]
-[MyCustomCollectionViewFlowLayout layoutAttributesForElementsInRect:]
-[UICollectionViewData validateLayoutInRect:]_block_invoke
-[UICollectionViewData validateLayoutInRect:]
-[UICollectionView layoutSubviews]
-[UIView(CALayerDelegate) layoutSublayersOfLayer:]
-[CALayer layoutSublayers]
似乎当我的自定义流布局代码调用 [self.collectionView numberOfItemsInSection:someSection] 来获取部分最后一项的布局属性时,该调用会根据新加载的数据返回(例如,在这种情况下,一个部分现在有 29 个项目)但默认流布局的内部仍在使用某种缓存数据(例如,在这种情况下,该部分只有 17 个项目)。不幸的是,我自己无法重现崩溃,即使是经历过崩溃的 Beta 测试人员也无法始终如一地重现。
知道那里发生了什么吗?
最佳答案
编辑 2,根据 2nd BenRB 的评论。
当 dataSource 更新并且您调用 reloadData 时,后者实际上会使 Collection View 中的所有内容无效。 然而,启动刷新过程的逻辑和确切顺序发生在默认流布局中,对我们是隐藏的。
特别地,默认流布局有它自己的私有(private)_prepareLayout(确切地说,带下划线)方法,它独立于prepareLayout 及其子类提供的重载.
prepareLayout(没有下划线)基本流布局类的默认实现什么都不做。
在刷新过程中,默认流布局使其子类有机会通过 layoutAttributesForElementsInRect: 和 layoutAttributesForItemAtIndexPath:“回调”提供更多信息(例如,额外的 layoutAttributes)。为了保证基类的数据和各自的 indexPath/layoutAttributes 数组之间的一致性,对相应“super”的调用应该只发生在这些各自的方法中:
[super layoutAttributesForElementsInRect:] 仅在
重载 [layoutAttributesForElementsInRect:]
[super layoutAttributesForItemAtIndexPath:] 仅在重载的 [layoutAttributesForItemAtIndexPath:] 中,
这些方法之间不应发生交叉调用,至少索引路径不是由它们相应的“ super ”方法提供的,因为我们不确切知道内部发生了什么。
我与我的 Collection View 斗争了很长时间,最终以唯一的工作序列结束:
通过直接访问数据源(无需 Collection View 的 numberOfItemsInSection: 的中介)来准备附加布局数据,并将该数据存储在子类的对象中,例如在字典属性中,使用 indexPath 作为键。我在重载的 [prepareLayout] 中执行此操作。
当基类通过回调请求此信息时,将存储的布局数据提供给基类:
//layoutAttributesForElementsInRect
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
//calls super to get the array of layoutAttributes initialised by the base class
**NSArray *array = [super layoutAttributesForElementsInRect:rect];**
for(MyLayoutAttributes *la in array)
if(la.representedElementCategory == UICollectionElementCategoryCell ){
NSIndexPath indexPath = la.indexPath //only this indexPath can be used during the call to layoutAttributesForItemAtIndexPath:!!!
//extracts custom layout data from a layouts dictionary
MyLayoutAttributes *cellLayout = layouts[la.indexPath];
//sets additional properties
la.this = cellLayout.this
la.that = cellLayout.that
...
}
....
return array;
}
//layoutAttributesForItemAtIndexPath:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
MyLayoutAttributes *la = (MyLayoutAttributes *)[super layoutAttributesForItemAtIndexPath:indexPath ];
if(la.representedElementCategory == UICollectionElementCategoryCell ){
NSIndexPath indexPath = la.indexPath //only this indexPath can be used during the call !!!
//extracts custom layout data from a layouts dictionary using indexPath as a key
MyLayoutAttributes *cellLayout = layouts[la.indexPath];
//sets additional properties
la.this = cellLayout.this
la.that = cellLayout.that
}
return la;
}
关于ios - UICollectionViewFlowLayout 子类在滚动和重新加载时崩溃访问超出范围的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24616797/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
当我在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)(人们推荐的最少