IOS學(xué)習(xí)之 -- UICollectionView

1、實(shí)現(xiàn)代理

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

2、聲明UICollectionView

@property (nonatomic, strong) UICollectionView *myCollectionView;

3、實(shí)現(xiàn)代理方法

/**
  *數(shù)據(jù)源代理
  */
//返回個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 50;
}

//返回單元格,自定義單元格很重要的部分
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    imageView.image = [UIImage imageNamed:@"image.jpg"];
    [cell.contentView addSubview:imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 35, 100, 40)];
    label.text = @"路飛";
    label.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:label];

    return cell;
}

 /**
  *布局代理
  */
//設(shè)置邊距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

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

//設(shè)置item寬高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(self.myCollectionView.bounds.size.width, 80);
}

//選擇item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    NSLog(@"選中了---> %lu", indexPath.row);
}

//點(diǎn)擊后的item
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
}

4、綁定View

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    //布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //創(chuàng)建CollectionView
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(20, 30, self.view.bounds.size.width-40, self.view.bounds.size.height-60) collectionViewLayout:flowLayout];

    //注冊(cè)Cell單元
    [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cid];

    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.myCollectionView];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,007評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,551評(píng)論 19 139
  • 1. 官網(wǎng)下載 Mysql 版本,解壓到D盤(pán)。 2. 安裝目錄下有個(gè)文件夾:my-default.ini ,重命名...
    蓋文哥閱讀 288評(píng)論 0 0
  • 惟木從繩則正,后從諫則圣。 《尚書(shū)·說(shuō)命上》 句意:木依從繩墨砍削就會(huì)正直,君主依從諫言行事就會(huì)圣明。 后:君王。
    xcy無(wú)名閱讀 2,743評(píng)論 0 0
  • 我,住在上海,但我并不是上海人。 當(dāng)初選擇在這個(gè)城市定居,有很大一部分原因是因?yàn)檫@個(gè)城市的建筑。我很喜歡這些精致并...
    弈詞閱讀 324評(píng)論 0 1

友情鏈接更多精彩內(nèi)容