Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView显示两行,但我只需要一行_Ios_Objective C_Uicollectionview - Fatal编程技术网

Ios UICollectionView显示两行,但我只需要一行

Ios UICollectionView显示两行,但我只需要一行,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我在使用UICollectionView时遇到了一个奇怪的错误,请看下图: 第三个单元格似乎以某种方式显示在第二个单元格下,而第三个单元格的位置则是空的 当我滚动视图,然后再向后滚动(或重新加载视图)时,它变为右侧: 以下是我使用的代码: #pragma mark - UICollectionView Datasource - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSIntege

我在使用UICollectionView时遇到了一个奇怪的错误,请看下图:

第三个单元格似乎以某种方式显示在第二个单元格下,而第三个单元格的位置则是空的

当我滚动视图,然后再向后滚动(或重新加载视图)时,它变为右侧:

以下是我使用的代码:

#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {

    return datas.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
 }

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:    (NSIndexPath *)indexPath {

    static NSString *cellId = @"floorCell";
    FloorCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellId     forIndexPath:indexPath];
    NSDictionary *dic = [datas objectAtIndex:indexPath.row];
    cell.goodImageView.imageURL = [NSURL URLWithString:[dic objectForKey:@"img"]];
    cell.goodLabel.text = [dic objectForKey:@"title"];
    return cell;

}
我使用的布局是UICollectionViewFlowLayout,布局的代理如下所示:

#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize retval = CGSizeMake(172 / 2.0, [FloorCollectionViewCell getHeight]);
    return retval;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
我想知道原因以及如何解决这个问题?谢谢

初始化collectionView的代码:

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(50, 0, 278, bottomView.frame.size.height)  collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerClass:[FloorCollectionViewCell class] forCellWithReuseIdentifier:@"floorCell"];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [bottomView addSubview:collectionView];
ps:我想我已经解决了这个问题,将collectionView的高度改为单元格的高度,而不是整个灰色超级视图的高度。但我仍然不知道原因,灰色的超视场的高度不够大,无法容纳两个单元格

更改的代码:

collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(50, 0, 278, [FloorCollectionViewCell getHeight])  collectionViewLayout:layout];

我不知道回答这个问题是否还有意义,但为了将来的缘故,您只需在集合视图本身设置高度约束即可解决此问题,这将强制集合视图不进入多行模式。这通常发生在您设置自动布局时具有不同优先级的约束,并且集合视图有足够的扩展空间。

请在初始化集合的位置(可能在viewdidload中)发布代码。@Thapa我已附加了代码