日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发- UICollectionView详解+实例

發布時間:2025/4/5 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发- UICollectionView详解+实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本章通過先總體介紹UICollectionView及其常用方法,再結合一個實例,了解如何使用UICollectionView。

?

UICollectionView 和 UICollectionViewController 類是iOS6 新引進的API,用于展示集合視圖,布局更加靈活,可實現多列布局,用法類似于UITableView 和 UITableViewController 類。

使用UICollectionView 必須實現UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout這三個協議。

?

下面先給出常用到的一些方法。(只給出常用的,其他的可以查看相關API)?

  • #pragma?mark?--?UICollectionViewDataSource???
  • //定義展示的UICollectionViewCell的個數??
  • -(NSInteger)collectionView:(UICollectionView?*)collectionView?numberOfItemsInSection:(NSInteger)section??
  • {??
  • ????return?30;??
  • } ??
  • //定義展示的Section的個數??
  • -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView?*)collectionView??
  • {??
  • ????return?1;??
  • } ??
  • //每個UICollectionView展示的內容??
  • -(UICollectionViewCell?*)collectionView:(UICollectionView?*)collectionView?cellForItemAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????static?NSString?*?CellIdentifier?=?@"GradientCell";??
  • ????UICollectionViewCell?*?cell?=?[collectionView?dequeueReusableCellWithReuseIdentifier:CellIdentifier?forIndexPath:indexPath];??
  • ??
  • ????cell.backgroundColor?=?[UIColor?colorWithRed:((10?*?indexPath.row)?/?255.0)?green:((20?*?indexPath.row)/255.0)?blue:((30?*?indexPath.row)/255.0)?alpha:1.0f];??
  • ????return?cell;??
  • } ??
  • #pragma?mark?--UICollectionViewDelegateFlowLayout???
  • //定義每個UICollectionView?的大小??
  • -?(CGSize)collectionView:(UICollectionView?*)collectionView?layout:(UICollectionViewLayout*)collectionViewLayout?sizeForItemAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????return?CGSizeMake(96,?100);??
  • } ??
  • //定義每個UICollectionView?的?margin??
  • -(UIEdgeInsets)collectionView:(UICollectionView?*)collectionView?layout:(UICollectionViewLayout?*)collectionViewLayout?insetForSectionAtIndex:(NSInteger)section??
  • {??
  • ????return?UIEdgeInsetsMake(5,?5,?5,?5);??
  • } ??
  • #pragma?mark?--UICollectionViewDelegate???
  • //UICollectionView被選中時調用的方法??
  • -(void)collectionView:(UICollectionView?*)collectionView?didSelectItemAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????UICollectionViewCell?*?cell?=?(UICollectionViewCell?*)[collectionView?cellForItemAtIndexPath:indexPath];??
  • ????cell.backgroundColor?=?[UIColor?whiteColor];??
  • } ??
  • //返回這個UICollectionView是否可以被選擇??
  • -(BOOL)collectionView:(UICollectionView?*)collectionView?shouldSelectItemAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????return?YES;??
  • }??
  • ?

    ?

    ?

    下面通過一個例子具體介紹下。(例子來自網絡。但是是通過第三方獲得的,無法取得鏈接。還望見諒。)

    ?

    iOS CollectionView的出現是一大福利,再也不用用TableView來定義復雜的多欄表格了,用法與Table類似,只是Cell必須自己添加,無默認模式

    由于CollectionView沒有默認的Cell布局,所以一般還是自定義方便又快捷

    一、自定義Cell

    1、新建類CollectionCell繼承自UICollectionViewCell

    2、新建Xib,命名為CollectionCell.xib

    a.選中CollectionCell.xib刪掉默認的View,從控件中拖一個Collection View Cell(圖3)到畫布中,設置大小為95*116;

    ?

    b.選中剛剛添加的Cell,更改類名為CollectionCell,如圖4

    c.在CollectionCell.xib的CollectionCell中添加一個ImageView和一個Label(圖5)

    d.創建映射, 圖6,圖7

    e.選中CollectionCell.m , 重寫init方法?

  • -?(id)initWithFrame:(CGRect)frame??
  • {??
  • ????self?=?[super?initWithFrame:frame];??
  • ????if?(self)??
  • ????{??
  • ????????//?初始化時加載collectionCell.xib文件??
  • ????????NSArray?*arrayOfViews?=?[[NSBundle?mainBundle]?loadNibNamed:@"CollectionCell"?owner:self?options:nil];??
  • ??????????
  • ????????//?如果路徑不存在,return?nil??
  • ????????if?(arrayOfViews.count?<?1)??
  • ????????{??
  • ????????????return?nil;??
  • ????????}??
  • ????????//?如果xib中view不屬于UICollectionViewCell類,return?nil??
  • ????????if?(![[arrayOfViews?objectAtIndex:0]?isKindOfClass:[UICollectionViewCell?class]])??
  • ????????{??
  • ????????????return?nil;??
  • ????????}??
  • ????????//?加載nib??
  • ????????self?=?[arrayOfViews?objectAtIndex:0];??
  • ????}??
  • ????return?self;??
  • }??

  • f.選中CollectionCell.xib 修改其identifier為CollectionCell。


    二、定義UICollectionView;

    1、拖動一個Collection View到指定ViewController的View上

    2、連線dataSource和delegate,并創建映射,命名為CollectionView

    3、選中CollectionView的標尺,將Cell Size的Width和Height改成與自定義的Cell一樣的95*116,圖8

    ? ??

    4、選中CollectionView的屬性,可以修改其屬性,比如是垂直滑動,還是水平滑動,選擇Vertical或Horizontal

    5、選中CollectionViewCell,修改Class,繼承自CollectionCell

    5、在ViewDidLoad方法中聲明Cell的類,在ViewDidLoad方法中添加,此句不聲明,將無法加載,程序崩潰

    其中,CollectionCell是這個Cell的標識(之前幾步已經定義過了。 )?

  • [self.collectionView?registerClass:[CollectionCell?class]?forCellWithReuseIdentifier:@"CollectionCell"];??

  • 6、在ViewController.h中聲明代理?

  • @interface?ViewController?:?UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>??


  • ?

    7、在.m文件中實現代理方法?

  • //每個section的item個數??
  • -(NSInteger)collectionView:(UICollectionView?*)collectionView?numberOfItemsInSection:(NSInteger)section??
  • {??
  • ????return?12;??
  • }??
  • ??
  • -(UICollectionViewCell?*)collectionView:(UICollectionView?*)collectionView?cellForItemAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ??????
  • ????CollectionCell?*cell?=?(CollectionCell?*)[collectionView?dequeueReusableCellWithReuseIdentifier:@"CollectionCell"?forIndexPath:indexPath];??
  • ??????
  • ????//圖片名稱??
  • ????NSString?*imageToLoad?=?[NSString?stringWithFormat:@"%d.png",?indexPath.row];??
  • ????//加載圖片??
  • ????cell.imageView.image?=?[UIImage?imageNamed:imageToLoad];??
  • ????//設置label文字??
  • ????cell.label.text?=?[NSString?stringWithFormat:@"{%ld,%ld}",(long)indexPath.row,(long)indexPath.section];??
  • ??????
  • ????return?cell;??
  • }??


  • ?

    8 。效果如圖10

    點擊某項后跳轉事件與UITableView類似,實現代理方法?

  • -(void)collectionView:(UICollectionView?*)collectionView?didSelectItemAtIndexPath:(NSIndexPath?*)indexPath ?
  • 總結

    以上是生活随笔為你收集整理的iOS开发- UICollectionView详解+实例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: av导航福利 | 成人黄色网址在线观看 | 国产性生活视频 | 久久久com | 五月天综合激情 | 成人黄网免费观看视频 | 久久先锋 | 黄站在线观看 | 国产靠逼网站 | 国产三级影院 | 日韩无码精品一区二区 | 成人午夜黄色 | 亚洲精品乱码久久久久久蜜桃不卡 | 91嫩草入口| 亚洲熟女乱色综合亚洲小说 | 久久精品性爱视频 | 少妇又色又爽又黄的视频 | 一区二区视频国产 | 四虎影视免费 | 在线你懂得 | 欧美a免费 | 成人做受黄大片 | 91狠狠爱 | 日韩欧美成人网 | 久久国产精品久久 | 亚洲AV无码AV吞精久久中文版 | 一区二区国产在线 | 国产精品无码在线播放 | 麻豆md0034在线观看 | 国产日韩欧美视频在线 | 精品黄色一级片 | 爱情岛论语亚洲入口 | 黄色在线观看免费视频 | 午夜诱惑痒痒网 | 国产成人专区 | 欧美激情一区二区 | 天天视频黄 | 国产乱码精品一区二区三区五月婷 | 99热网站 | 欧美高清一区二区 | www.日韩| 亚洲黄色一区 | 丁香啪啪 | 亚洲精品久久久久久无码色欲四季 | 打美女白嫩屁屁网站 | 在线观看黄色av | 亚洲熟妇无码av | 蜜桃无码一区二区三区 | 久久国产人妻一区二区免色戒电影 | 天天弄天天干 | 免费看国产一级片 | 午夜一区二区三区在线观看 | 污污网站在线观看 | 日韩欧美一卡二卡 | 欧美激情综合网 | 色鬼综合| 久久精品男人的天堂 | 香蕉视频国产在线观看 | 高潮一区二区三区乱码 | 精品国产一区二区三区四区 | 人人人人干 | 日韩高清影院 | 色很久| 日本亚洲欧美在线 | 久久久久久少妇 | 免费久草视频 | 91美女视频网站 | 天天操天天操天天操 | 乱色熟女综合一区二区三区 | 性高潮影院 | wwwxxx亚洲| 国产日韩高清在线 | 九一精品视频 | www.超碰97| 99天堂网| 欧洲色视频 | 国产偷自拍视频 | 免费一级特黄3大片视频 | 产乳奶汁h文1v1 | 亚洲免费网站在线观看 | 日韩欧美中文字幕在线视频 | 岛国av不卡 | 国产精品另类 | 国产精品第七页 | 亚洲天堂影院 | 人人干97| 浪漫樱花在线观看高清动漫 | 欧美一区二区视频在线 | 宅男噜噜噜| 在线免费日韩av | 免费的黄色网址 | 日日干天天 | 婷婷.com | 免费成人av | 国产小视频91 | 自拍视频一区 | 性欧美大战久久久久久久 | 片黄在线观看 | julia一区二区三区在线观看 |