簡體   English   中英

如何以編程方式刪除UICollectionView中行之間的間距?

[英]How to remove spacing between lines in UICollectionView programmatically?

我有一個UICollectionView ,行之間的間距為0。 我在.h類中添加了UICollectionViewDelegateFlowLayout並實現了

(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section**

方法將它們設置為返回0.0。 同樣在ViewDidLoad我動態創建了UICollectionViewFlowLayoutUICollectionView 對於UICollectionViewFlowLayout我將minimumInteritemSpacingminimumLineSpacing設置為0,但行之間的間隔仍大於0。

這是我的代碼:

- (void)initializeCollectionView {

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(50, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumLineSpacing:0.0];

flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;

collView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-70) collectionViewLayout:flowLayout];
[collView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

[collView setBackgroundColor:[UIColor grayColor]];

collView.delegate = self;
collView.dataSource = self;

[[self view] addSubview:collView];

}




- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 1;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return 100;

}



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

return 0.0;

}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

return 0.0;

 }


- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

return UIEdgeInsetsMake(0, 0, 0, 0);

}

謝謝您的回答。

我做了一些不同的事情。 我以編程方式創建了一個collectionView並將其添加到視圖控制器中。 在我的CollectionView自定義類中,我進行了所有自定義。

我的自定義收藏夾視圖界面文件-

#import <UIKit/UIKit.h>
#import "CustomCell.h"


@interface CustomCollectionView : UIView<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate>

    @property(nonatomic, strong) UICollectionView *myCollectionView;

@end

現在在實現File(CustomCollectionView.m)中,首先我必須初始化並添加我的collectionView。

繪制視圖后立即調用initWithFrame 因此,我在該視圖內添加了collectionView,並讓該視圖處理所有委托方法以自定義我的集合視圖並通過數據源方法填充數據。

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, frame.size.width, frame.size.height) collectionViewLayout:flowLayout];
        self.myCollectionView.backgroundColor=[UIColor whiteColor];

        self.myCollectionView.delegate=self;
        self.myCollectionView.dataSource=self;

        [self.myCollectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"customCell"];

        [self addSubview:self.myCollectionView];
    }
    return self;
}

現在,讓我們實現UICollectionViewDelegateFlowLayout方法

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    //here I just made sure that I have square spaces boxes as my cell size
    int width = self.myCollectionView.frame.size.width/5;
    int height = width;
    return CGSizeMake(width, height);
}

現在,UICollectionView數據源

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

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

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

    CustomCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"customCell" forIndexPath:indexPath];
    if(cell){
       //customize the cell here
       return cell;
    }
}

現在,當我准備好帶有collectionView的自定義視圖時,就將其添加到了viewController中。

- (void)viewDidLoad {
    [super viewDidLoad];

    CustomCollectionView *myCollectionView = [[CustomCollectionView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview: myCollectionView];

}

以上為我工作。 您可以使用情節提要,但是當我嘗試自定義由IBOutlet連接的collectionView時,發現了一些麻煩。 因此,我采用了這種方法。 可能有更好的方法可以做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM