溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

UI中CollectionView的創(chuàng)建與使用

發(fā)布時間:2020-07-27 15:37:15 來源:網(wǎng)絡 閱讀:455 作者:hmymy 欄目:開發(fā)技術

在.h中聲明

@property (nonatomic, strong) UICollectionView *myCollecionView;



遵循CollectionView協(xié)議

<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>


.m文件中實現(xiàn)

@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    _collectionView.delegate = self;

    _collectionView.dataSource = self;

    

    

    //創(chuàng)建布局對象

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    

    //itmeitem之間的最小間距--默認是10

//    flowLayout.minimumInteritemSpacing = 110;

//    flowLayout.minimumLineSpacing = 100;

//    flowLayout.itemSize = CGSizeMake(80, 80);

    

//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    

    

    //初始化CollectionView

    _myCollecionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 667 - 284, 375, 284) collectionViewLayout:flowLayout];

    _myCollecionView.tag = 200;

    _myCollecionView.delegate = self;

    _myCollecionView.dataSource = self;

    

    _myCollecionView.backgroundColor = [UIColor redColor];

    [self.view addSubview:_myCollecionView];

    

    

    //注冊單元格

    [_myCollecionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCollecionViewCell"];

    

    

}



#pragma mark -UICollectionViewDataSource

//指定組的個數(shù)

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 2;

}



//指定單元格的個數(shù)

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

{

    return 21;

}



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

{

    

    if (collectionView.tag == 100) {

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];

        

        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 0.1 alpha:1];

        

        return cell;

    }else if (collectionView.tag == 200) {

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollecionViewCell" forIndexPath:indexPath];

        

        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 0.1 alpha:1];

        

        return cell;

    }

    

    return nil;

}



//動態(tài)地設置單元格的尺寸

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(80, arc4random() % 80);

}





@end


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI