UICollectionView 单元格布局超出了集合视图的边界 [英] UICollectionView cell layout is out of the collection view's bounds

查看:48
本文介绍了UICollectionView 单元格布局超出了集合视图的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView,它使用 Flow Layout 来布置它的单元格.它是垂直滚动的.

I have a UICollectionView which lays its cells out using Flow Layout. It is vertically scrolling.

它填充一个 320x480 的屏幕并显示一个始终为 96*96 的自定义 UICollectionViewCell - 大小在 Interface Builder 中设置,并且委托没有实现基于每个项目设置项目大小的方法.

It fills a 320x480 screen and displays a custom UICollectionViewCell which is always 96*96 - the size is set in Interface Builder and the delegate does not implement methods to set the item size on a per item basis.

左右各有 10 px 的插图,最小间距设置为 6 px.

There are insets on the left and right which are 10 px each and the minimum spacing is set to 6px.

因此,每条水平线有 3 个单元格.

What happens therefore is 3 cells per horizontal line.

<-10-><----96----><-6-><----96----><-6-><----96----><-10-> = 320.

<-10-><----96----><-6-><----96----><-6-><----96----><-10-> = 320.

我遇到的问题是偶尔会在一行上放置 4 个单元格!第 4 个单元格大部分不在屏幕上.然后它只在下一行放置 2 个单元格以进行补偿.流布局的重点在于它是一种换行布局,不应将任何内容放在屏幕外!

The problem that I am having is that occasionally it lays 4 cells on one line! The 4th cell is mostly off the screen. It then lays only 2 cells out on the following line to compensate. The whole point of flow layout is that it is a line-breaking layout which should not put anything off-screen!

我附上了一张图片:

请注意,在第二行有第四个项目,大部分不在屏幕上.

Please note that on the second row there is a fourth item mostly off the screen.

我真的不知道是什么导致了它.单元格动态填充数据,但它们的大小是恒定的.所有其他行上的单元格都很好,所以没有理由为什么在某些行上会发生这种情况.

I really have no idea what could be causing it. The cells are dynamically filled with data but their size is constant. The cells on all other rows are fitting just fine so there is no reason why on some rows this should happen.

每次更新集合视图时,都会在不同的地方发生此错误.IE.它可能发生在第 2 行,然后一旦发生更新,它可能发生在第 10 行,依此类推.

This error occurs at different places each time the collection view is updated. ie. it might happen at row 2 then once an update happens it might happen at row 10 and so on.

集合视图正在由计时器更新.计时器本质上调用一个函数来处理一些数据,然后使用 dispatch_async(到主队列)更新一个包含后备数据的数组,并调用 [collectionView reloadData].所以我没有从主线程之外的任何线程更新集合视图.后备数组仅在主队列和该函数中更新;不是来自其他任何地方.

The collection view is being updated by a timer. The timer essentially calls a function which processes some data, and then using dispatch_async (onto the main queue) an array containing the backing data is updated and [collectionView reloadData] is called. So I haven't updated the collection view from any thread apart from the main thread. The backing array is only updated on the main queue and in that function; not from anywhere else.

请有人给我一些关于可能出错的建议.

Please could someone give me some suggestions as to what might be going wrong.

谢谢

推荐答案

我已经通过在 scrollViewDidScroll: 委托方法中添加 [_collectionViewFlowLayout invalidateLayout] 来修复它.

I have fixed it by adding [_collectionViewFlowLayout invalidateLayout] in scrollViewDidScroll: delegate methods.

https://github.com/f33chobits/FSCalendar/issues/21

这篇关于UICollectionView 单元格布局超出了集合视图的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆