我有一个用于显示缩略图的 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/