[英]iOS UICollectionView skips data for cells
When assigning data to my UICollectionViewCell
it gets added but leaves a blank Cell between them. 将数据分配给我的UICollectionViewCell
,会添加它,但在它们之间留有一个空白单元格。
When the UICollectionView
is displayed I expect results to be: 当显示UICollectionView
时,我希望结果是:
[1] [2] [3] [4] and so on [1] [2] [3] [4]等
But instead I get this: 但是我得到了这个:
[1] [ ] [2] [ ] [3] [ ] [4] [1] [] [2] [] [3] [] [4]
Code from viewDidLoad
: 来自viewDidLoad
代码:
- (void) viewDidLoad {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 70) collectionViewLayout:layout];
_collectionView.userInteractionEnabled = YES;
_collectionView.alwaysBounceHorizontal = YES;
_collectionView.multipleTouchEnabled = NO;
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView setPagingEnabled:NO];
[_collectionView setScrollEnabled:YES];
[_collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"CustomCell"];
}
Code from cellForItemAtIndexPath
: 来自cellForItemAtIndexPath
代码:
- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; //testing row
return cell;
}
And my CustomCell.m
: 还有我的CustomCell.m
:
- (id)initWithFrame:(CGRect)aRect
{
self = [super initWithFrame:aRect];
{
titleLabel = [[UILabel alloc] initWithFrame:self.frame];
[self addSubview:titleLabel];
}
return self;
}
You should try to implement one of these (if not, all) methods: 您应该尝试实现以下一种方法(如果不是全部):
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
-(CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
-(CGFloat) collectionView:(UICollectionView *) collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
The UICollectionViewDelegateFlowLayout is responsible with the sizes and the spacing between cells in a UICollectionView. UICollectionViewDelegateFlowLayout负责UICollectionView中单元格的大小和间距。 Find out more here . 在这里找到更多。
Nevertheless, if you want to fully solve and find out which is the main problem, you should debug your views by pressing the 7th button on the Xcode's bottom bar(I have the 7.2 version). 但是,如果您想完全解决并找出主要问题,则应按Xcode底部栏上的第7个按钮(我的版本为7.2)来调试视图。 Here is a snapshot: 这是快照:
And this is how it should look the debugging: 这就是调试的样子:
Wow I could solve this! 哇,我可以解决这个问题!
I've logged the Subviews of the CustomCell
and there is a bug I think when assigning the frame to UILabel
. 我已经记录了CustomCell的子CustomCell
并且在将框架分配给UILabel
时出现了一个错误。
Log for titleLabel = [[UILabel alloc] initWithFrame:self.frame]
: 记录titleLabel = [[UILabel alloc] initWithFrame:self.frame]
:
App( "<UIView: 0x7b74cb00; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7b74d0b0>; layer = <CALayer: 0x7b74cbc0>>", "<UILabel: 0x7b74d0e0; frame = (0 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7b74d1f0>>" ) App ( "<UIView: 0x7c954cf0; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7c919a10>; layer = <CALayer: 0x7c9f30e0>>", "<UILabel: 0x7c985bc0; frame = (55 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c952020>>" )
As you can see the frame of the UILabel
gets pushed right. 如您所见, UILabel
的框架被正确推入。
Log for titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 70)]
: 记录titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 70)]
:
App ( "<UIView: 0x7c2a1f00; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7c2924f0>; layer = <CALayer: 0x7c2a4970>>", "<UILabel: 0x7c2a3c60; frame = (0 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c292520>>" ) App ( "<UIView: 0x7c2a3f30; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7c2a5ce0>; layer = <CALayer: 0x7c2a3ff0>>", "<UILabel: 0x7c2a5d10; frame = (0 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c2a5e20>>" )
Now the frame of the UILabel
is exactly the same as the UICollectionViewCell
respectively as the UIView
. 现在, UILabel
的框架分别与UICollectionViewCell
和UIView
完全相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.