如何设置集合单元视图以动态拉伸到
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