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

如何设置集合单元视图以动态拉伸到 iphone屏幕宽度(例如 iphone 5s,iphone 6 plus)?

我试过了:

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

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

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

    return cell;
}

这不起作用.我没有看到内容延伸到屏幕的右侧.

我试过添加这个委托方法

- (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);
}

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

此外,没有必要在 – (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中设置单元格边界,因为大小通过 – (CGSize)collectionView设置:(UICollectionView *)collectionView布局:( UICollectionViewLayout *)collectionViewLayout sizeforItemAtIndexPath:(NSIndexPath *)indexPath

还要确保您已连接所需的代表和DataSources到UICollectionView

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...