objective-c - iOS 6 Collection View 格式问题

标签 objective-c ios uicollectionview

我有一个用于显示缩略图的 Collection View 。我希望它像照片应用程序一样有 4 列。

我有一些示例代码(还没有太多),它让我对它所做的数学感到困惑。

我正在使用一个数组(thumbs),该数组是通过 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;

}`

最佳答案

Collection View 的要点是您不需要进行这些计算。使用 UICollectionViewFlowLayout 作为您的布局类,使单元格具有适当的大小(因此一行中只能容纳 4 个单元格),并有一个包含所有缩略图的部分。流布局将通过自动调整单元格的位置来填充行来为您提供网格。

关于objective-c - iOS 6 Collection View 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12879780/

相关文章:

ios - 如何从 UICollectionView 过渡到 UIViewController,如 Pinterest/Evernote

ios - 重新排列 UICollectionView 的单元格

iOS:如何以编程方式从应用程序中的私钥和 x509 证书创建 PKCS12 (P12) keystore ?

objective-c - 在 Swift 中公开 Objective C 类扩展属性

iphone - iPhone应用程序的退出点是什么

iphone - 在 iOS4 中,让 NSTimer 与 NSRunLoop 一起运行会禁用触摸/手势输入

ios - 何时正确存储或删除用户设备 token

objective-c - iOS6 中的 iOS 自定义电子邮件附件处理

ios - XMPPFramework - 不断收到错误代码 503 服务不可用

ios - Swift iOS -NavigationBar hidesBarsOnSwipe 在状态栏下设置 CollectionView Frame 时永远不会重新出现