iOS UICollectionView会跳过单元格的数据

时间:2016-01-07 09:37:31

标签: ios objective-c uicollectionview uicollectionviewcell

将数据分配给我的UICollectionViewCell时,会添加数据但会在它们之间留下一个空格。

显示UICollectionView时,我希望结果为:

[1] [2] [3] [4]等等

但我得到了这个:

[1] [] [2] [] [3] [] [4]

来自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"];
}

来自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;
}

我的CustomCell.m

- (id)initWithFrame:(CGRect)aRect
{
    self = [super initWithFrame:aRect];
    {
        titleLabel = [[UILabel alloc] initWithFrame:self.frame];

        [self addSubview:titleLabel];
    }
    return self;
}

2 个答案:

答案 0 :(得分:0)

您应该尝试实现其中一种(如果不是全部)方法:

-(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{

UICollectionViewDelegateFlowLayout 负责UICollectionView中单元格的大小和间距。了解更多here

然而,如果你想完全解决并找出哪个是主要问题,你应该按Xcode底部栏上的第7个按钮调试你的视图(我有7.2版本)。这是一个快照:

Snapshot

这就是它应该如何调试:

Debugging

答案 1 :(得分:0)

哇我能解决这个问题!

我已经记录了CustomCell的子视图,我认为在将框架分配给UILabel时会出现错误。

记录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>>"
)
&#13;
&#13;
&#13;

正如您所见,UILabel的框架被推向右侧。

记录titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 70)]

&#13;
&#13;
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>>"
)
&#13;
&#13;
&#13;

现在,UILabel的框架与UICollectionViewCell的框架分别与UIView完全相同。