ios - 如何将集合单元格宽度设置为动态拉伸(stretch)到手机宽度

标签 ios objective-c

如何将集合单元格 View 设置为动态拉伸(stretch)到 iphone 屏幕宽度(例如 iphone 5s、iphone 6 plus)?

我试过:

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

    (ResultCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellClass
                                                                      forIndexPath:indexPath];

    cell.bounds = CGRectMake(0,0, self.view.bounds.size.width, 150);

    return cell;
}

那是行不通的。我没有看到内容拉伸(stretch)到屏幕的右侧。

我试过添加这个委托(delegate)方法:

- (CGSize)collectionView:(UICollectionView *)collectionView
            layout:(UICollectionViewLayout *)collectionViewLayout
            sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize;

    cellSize.width = self.view.bounds.size.width;

    // body view height
    cellSize.height = 150;

    return cellSize;
}

我在方法中设置了断点,但该方法从未被调用?

最佳答案

我遇到了相同用例的相同问题。我终于结束了

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.bounds.size.width, 150);
}

这会更改 Collection View 中每个项目(单元格)的大小。因此,您可以使用它更改每个单元格的大小。在这里我需要单元格宽度与我的 UICollectionView 相同,所以我传递了我想要的 UICollectionview 的宽度和特定高度。希望这会有所帮助。

也不需要在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 中设置单元格边界,因为大小是通过 - 设置的(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

还要确保您已将所需的 Delegates & DataSources 附加到 UICollectionView

关于ios - 如何将集合单元格宽度设置为动态拉伸(stretch)到手机宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117449/

相关文章:

ios - 我的应用程序在后台时需要获取我的位置

ios - ViewController 中多个 CollectionView 的滚动冲突

ios - 如何获取iOS设备名称

objective-c - 如何在 iPhone 上将 NSMutableArray 转换为 CSV 文件?

ios - 在 Objective-C 中使用 JSON 中的 webService

iphone - NSNotification ...正确的方法?

ios - 使用 Accelerate 平均像素颜色

ios - 将之前计算的数据添加到每次按下 UIButton 时生成的新数据中

ios - 每次使用 FPPopoverController 退出带有弹出窗口的 View 时,它都会 NSLogs "FPPopover dealloc",我怎样才能阻止这个?

iOS 如何像在 iPhone 照片应用程序中那样在照片之间的日期 Collection View 中放置标题