Ios UICollectionView更改单元格顶部和底部距离

Ios UICollectionView更改单元格顶部和底部距离,ios,objective-c,iphone,uicollectionview,Ios,Objective C,Iphone,Uicollectionview,我对IOS开发相当陌生。我正在使用本教程中的代码。您可以从中获得代码。这里我想更改集合视图单元格中的图像顶部和底部距离。我怎么能做到? 以下是集合视图单元格的代码: -(void)setupCollectionView { [self.collectionView registerClass:[CMFGalleryCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; UICollectionViewFlowLayo

我对IOS开发相当陌生。我正在使用本教程中的代码。您可以从中获得代码。这里我想更改集合视图单元格中的图像顶部和底部距离。我怎么能做到? 以下是集合视图单元格的代码:

-(void)setupCollectionView {
    [self.collectionView registerClass:[CMFGalleryCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setMinimumInteritemSpacing:0.0f];
    [flowLayout setMinimumLineSpacing:0.0f];
    [self.collectionView setPagingEnabled:YES];
    [self.collectionView setCollectionViewLayout:flowLayout];
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.dataArray count];
}

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

    CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
    [cell setImageName:imageName];
    [cell updateCell];

    return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.collectionView.frame.size;
}


#pragma mark -
#pragma mark Data methods
-(void)loadImages {

    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Assets"];
    self.dataArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL];

}
CMFGallary.cell的代码为:

#import "CMFGalleryCell.h"

@interface CMFGalleryCell()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation CMFGalleryCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CMFGalleryCell" owner:self options:nil];

        if ([arrayOfViews count] < 1) {
            return nil;
        }

        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
            return nil;
        }

        self = [arrayOfViews objectAtIndex:0];
    }

    return self;
}

-(void)updateCell {

    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Assets"];
    NSString *filename = [NSString stringWithFormat:@"%@/%@", sourcePath, self.imageName];

    UIImage *image = [UIImage imageWithContentsOfFile:filename];

    [self.imageView setImage:image];

    [self.imageView setContentMode:UIViewContentModeScaleAspectFit];

}

@end
#导入“CMFGalleryCell.h”
@接口CMFGalleryCell()
@属性(强,非原子)IBUIImageView*imageView;
@结束
@CMFGalleryCell的实现
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
NSArray*ArrayOfView=[[NSBundle mainBundle]loadNibNamed:@“CMFGalleryCell”所有者:自选项:nil];
如果([arrayOfViews计数]<1){
返回零;
}
如果(![[arrayOfViews objectAtIndex:0]是类的种类:[UICollectionViewCell类]]){
返回零;
}
self=[ArrayOfView对象索引:0];
}
回归自我;
}
-(void)updateCell{
NSString*sourcePath=[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“资产”];
NSString*文件名=[NSString stringWithFormat:@“%@/%@”,源路径,self.imageName];
UIImage*image=[UIImage imageWithContentsOfFile:filename];
[self.imageView setImage:image];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
@结束

为添加插图更新集合视图流布局,如:

UICollectionViewFlowLayout *flow = yourCollectionView.collectionViewLayout;
flow.sectionInset = UIEdgeInsetsMake(topMargin, leftMargin, bottom, right);
或实施

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(topMargin, leftMargin, bottom, right);
}

请在问题中直接填写相关代码。如果链接页面发生更改,链接可能会变得无效。是否可以复制/粘贴CMFGalleryCell.m中的代码?您可以在上面看到它…不,它不会更改具有集合视图图像的单元格…请帮助