在UICollectionView中每个部分显示两个单元格

时间:2014-03-26 03:49:20

标签: ios objective-c uicollectionview

我使用UICollectionView显示图像,我已经存储在这样的数组中。

newsPhotos = [NSArray arrayWithObjects:@"photo1.png", @"photo2.png", @"photo3.png", @"photo4.png", @"photo5.png", @"photo6.png", @"photo7.png", @"photo8.png", @"photo9.png", @"photo10.png", Nil];

我的想法是显示此订单上的网格

1 | 2
3 | 4
5 | 6
7 | 8
9 | 10

所以两张照片一行,我的UICollectionView方法如下所示:

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {

    return 2;
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return newsPhotos.count;
}

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

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.section]];

    cell.textLabel.text = @"Data";



    return cell;
}

但它的打印就像这样

1 | 1
2 | 2
3 | 3
etc..

我明白为什么会发生这种情况,并且不知道如何解决它。有什么帮助吗?

由于

4 个答案:

答案 0 :(得分:2)

试试这个。

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

-(NSInteger)collectionView:(UICollectionView *)collectionView
    numberOfItemsInSection:(NSInteger)section
{
    return newsPhotos.count;
}



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

    // Try and use custom cell by placing a custom cell on your collection view,assign a new class to that cell and then configure that cell as

      customCellClass *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    // also place an image view on your custom cell, make an outlet of that image view in your custom cell class and use:


        myCell.imageview.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row]];

        return myCell;
    }

现在每行显示两个图像,您可以相应地调整单元格和图像视图的大小,这样它每行只显示两个图像。 希望这会对你有所帮助

答案 1 :(得分:1)

尝试添加它以便:

  recipeImageView.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row + (indexPath.section *2)]];

当你这样做时,你会得到什么?

因为你想要做的是:

section == 0 + row == 0 *2  [0]  | section == 1 + row == 0 *2 [1]  
section == 0 + row == 1 *2  [2]  | section == 1 + row == 1 *2 [3]  
section == 0 + row == 2 *2  [4]  | section == 1 + row == 2 *2 [5]  
section == 0 + row == 3 *2  [6]  | section == 1 + row == 3 *2 [7]  

对不起,我正在做这件事,试试这个它会对你有用,我刚刚测试过。确保将滚动方向设置为垂直。

答案 2 :(得分:1)

问题在于您正在查看indexPath.section而不是indexPath.row,因为每个部分都有两个项目,它会像您在那里一样重复。

您可以尝试使用一个部分并使用[newsPhotos objectAtIndex:indexPath.row]代替。您还可以使用UICollectionViewFlowLayoutDelegate更改布局,如下所示:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.5f, cellHeightHere);
} 

您可以看到更详细的答案here

答案 3 :(得分:0)

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let size = collectionView.bounds.size.width / 2

    return CGSize(width: size, height: 262)

}