国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

iOSUICollectionView簡(jiǎn)單使用

2019-11-14 18:30:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

首先認(rèn)識(shí)一下UICollectionView

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView  

 

UICollectionView%20和%20UICollectionViewController%20類(lèi)是iOS6%20新引進(jìn)的API,用于展示集合視圖,布局更加靈活,可實(shí)現(xiàn)多列布局,用法類(lèi)似于UITableView%20和%20UITableViewController%20類(lèi)。

使用UICollectionView%20必須實(shí)現(xiàn)UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout這三個(gè)協(xié)議。

 

下面給出一些常用方法,具體的使用可以參考Demo:

- (void)viewDidLoad  
  • {  
  •     [super viewDidLoad];  
  •     self.title = @"UICollectionView學(xué)習(xí)";  
  •       
  •     //通過(guò)Nib生成cell,然后注冊(cè) Nib的view需要繼承 UICollectionViewCell  
  •     [self.collectionView registerNib:[UINib nibWithNibName:@"SQCollectionCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];  
  •       
  •     //注冊(cè)headerView Nib的view需要繼承UICollectionReusableView  
  •     [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];  
  •     //注冊(cè)footerView Nib的view需要繼承UICollectionReusableView  
  •     [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];  
  •     //  
  •     self.collectionView.allowsMultlong)indexPath.row];  
  •     imageView.image = [UIImage imageNamed:imageName];  
  •     label.text = imageName;  
  •       
  •     cell.backgroundColor = [UIColor redColor];  
  •     return cell;  
  •       
  • }  
  • // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:  
  • - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{  
  •       
  •     NSString *reuseIdentifier;  
  •     if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){  
  •         reuseIdentifier = kfooterIdentifier;  
  •     }else{  
  •         reuseIdentifier = kheaderIdentifier;  
  •     }  
  •       
  •     UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];  
  •       
  •     UILabel *label = (UILabel *)[view viewWithTag:1];  
  •     if ([kind isEqualToString:UICollectionElementKindSectionHeader]){  
  •         label.text = [NSString stringWithFormat:@"這是header:%d",indexPath.section];  
  •     }  
  •     else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){  
  •         view.backgroundColor = [UIColor lightGrayColor];  
  •         label.text = [NSString stringWithFormat:@"這是footer:%d",indexPath.section];  
  •     }  
  •     return view;  
  • }  
  • //定義每個(gè)UICollectionViewCell 的大小  
  • - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath  
  • {  
  •     return CGSizeMake(60, 80);  
  • }  
  • //定義每個(gè)Section 的 margin  
  • -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
  • {  
  •     return UIEdgeInsetsMake(15, 15, 5, 15);//分別為上、左、下、右  
  • }  
  • //返回頭headerView的大小  
  • -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{  
  •     CGSize size={320,45};  
  •     return size;  
  • }  
  • //返回頭footerView的大小  
  • - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section  
  • {  
  •     CGSize size={320,45};  
  •     return size;  
  • }  
  • //每個(gè)section中不同的行之間的行間距  
  • - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section  
  • {  
  •     return 10;  
  • }  
  • //每個(gè)item之間的間距  
  • //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
  • //{  
  • //    return 100;  
  • //}  
  • //選擇了某個(gè)cell  
  • - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  • {  
  •     UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
  •     [cell setBackgroundColor:[UIColor greenColor]];  
  • }  
  • //取消選擇了某個(gè)cell  
  • - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath  
  • {  
  •     UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
  •     [cell setBackgroundColor:[UIColor redColor]];  
  • }  

  • 效果圖如下:
    原文鏈接:http://blog.csdn.net/Apple_app/article/details/38867123

    上一篇:iOS開(kāi)發(fā)日記4-第三方登錄(ShareSDK)

    下一篇:iOS開(kāi)發(fā)-UI篇-AutoLayout

    發(fā)表評(píng)論 共有條評(píng)論
    用戶(hù)名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    學(xué)習(xí)交流
    熱門(mén)圖片
    猜你喜歡的新聞
    猜你喜歡的關(guān)注

    新聞熱點(diǎn)

    疑難解答

    圖片精選

    網(wǎng)友關(guān)注

    主站蜘蛛池模板: 泉州市| 河北省| 丹凤县| 吉隆县| 宁德市| 浏阳市| 永修县| 永仁县| 三河市| 贺州市| 巴塘县| 英吉沙县| 公主岭市| 海盐县| 阿尔山市| 阿拉善盟| 谢通门县| 甘肃省| 宣恩县| 全椒县| 新蔡县| 于都县| 民乐县| 额尔古纳市| 德清县| 新源县| 信丰县| 夏邑县| 泸定县| 体育| 安化县| 保定市| 婺源县| 电白县| 剑河县| 聂拉木县| 吉水县| 青田县| 繁峙县| 乐昌市| 曲水县|