【问题标题】:How to set UICollectionViewCell size如何设置 UICollectionViewCell 大小
【发布时间】:2017-02-24 06:49:23
【问题描述】:

我使用UICollectionView 并在单元格中设置图像。我想要一个单元格大小是width = view/2hight = view/3。和顶部margin =8right = 8left = 8。我附上了一张图片,请看那些。我想喜欢这个请看图片。

我的代码

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return img_ary.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";


category_cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.img_view.image = [UIImage imageNamed:[img_ary objectAtIndex:indexPath.row]];


return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// [self performSegueWithIdentifier:@"showRecipeDetails" sender:self];


}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0); //top, left, bottom, right);

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat widthOfCell =(self.view.frame.size.width-30)/2;
CGFloat heightOfCell =(self.view.frame.size.height-30)/4;
CGSize returnValue = CGSizeMake(widthOfCell, heightOfCell);
returnValue.height += 5;
returnValue.width += 5;
return returnValue;

}

IMAGE: I want to likes this please see

我尝试了很多次,并且使用了更多方法,但没有像附加图像那样设置单元格。请建议我该怎么做。谢谢你

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

    你试过了吗

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;{
    
        CGSize defaultSize = CGSizeMake((_collectionViewSongs.frame.size.width/2)-10, (_collectionViewSongs.frame.size.width/2)-10);
    
    
        return defaultSize;
    }
    

    【讨论】:

    • 如何设置两个单元格之间的间距?
    • 使用 edgeInset 。给 UIEdgeInsetsMake(0, 5, 0, 5);
    【解决方案2】:

    将左、右、上和下 0 分配给您的收藏视图

    使用下面的集合视图方法

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    int width = (collectionView.frame.size.width/2)-8;
    int height = (collectionView.frame.size.height/3)-8;
    return CGSizeMake(width, height);}
    

    【讨论】:

    • 如何设置两个单元格之间的间距?
    • 选择你的collectionview并进入尺寸检查器,你会发现Cell和Line的“Min Spacing”。你可以从这里改变间距
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    相关资源
    最近更新 更多