【问题标题】:iOS 6 Collection view formatting issueiOS 6 Collection 视图格式问题
【发布时间】:2012-10-14 06:15:50
【问题描述】:

我有一个用来显示缩略图的集合视图。我希望它是 4 列,就像照片应用一样。

我得到了一些示例代码(目前还没有太多),它正在做的数学让我感到困惑。

我正在使用一个数组(拇指),它是通过 NSXMLParser 从作为数据源的 Web 服务返回的 XML 构建的。

但是,由于该数组没有“numberOfSections”或“numberOfItemsInSection”(它只是一个带有照片名称和位置的简单二维数组),因此我无法确定调整它以处理 4 个跨度所需的数学。

例如:如果它是第三行/第二列,那么数组中的索引是 9(零基)吗? (因为 2 个完整行 * 每行 4 个 + 2(第二列)= 10)?.. 困惑

另请注意:当前代码将仅显示偶数 # 个图片。因此,如果某张专辑中有 3 张图片,则只会显示 2 张。

当前代码:

`#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    // Return the number of sections.
    return [thumbs count] / 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollCell" forIndexPath:indexPath];
    //get current photo from collection
    PhotoCollection *currentPhoto = [thumbs objectAtIndex:(indexPath.section*2 + indexPath.row)];

    //Show in PhotoName Cell
    cell.photoName.text = currentPhoto.PhotoName;

    //show thumb via:SDWebImage
    [cell.photoView setImageWithURL:[NSURL URLWithString:currentPhoto.PhotoThumbnailAbsoluteLocation] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    //regular (wasn't working by default)
    //cell.photoView.image = [UIImage imageNamed:currentPhoto.PhotoThumbnailAbsoluteLocation];

    return cell;

}`

【问题讨论】:

    标签: objective-c ios uicollectionview


    【解决方案1】:

    集合视图的意义在于您不需要进行这些计算。使用UICollectionViewFlowLayout 作为您的布局类,使单元格具有适当的大小(因此只有 4 个可以放在一行中)并有一个包含所有缩略图的部分。流布局将通过自动调整单元格的位置以填充行来为您提供网格。

    【讨论】:

    • 是的,我需要将部分设置为 1。我曾使用某人的示例代码作为开始,它的数学很有趣,例如 [myArray count] / 4 部分总...谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多