亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

iOS仿京東分類菜單之UICollectionView內容

2019-11-14 18:10:40
字體:
來源:轉載
供稿:網友

在上《iOS仿京東分類菜單實例實現》已經實現了大部分主體的功能,本文是針對右邊集合列表進行修改擴展,使它達到分組的效果,本文涉及到的主要是UICollectionView的知識內容,左邊列表的實現見上一篇文章,先看實現的效果圖:

一:實體的創建

1.1分組實體的創建(tagID跟左邊表格進行關聯,roomArray是存放房間的數組,也就是單元格的集合)

#import <Foundation/Foundation.h>@interface rightModel : NSObject//實體leftTageModel中的主鍵值@PRoperty(assign,nonatomic)long tagID;@property(assign,nonatomic)long roomStyleID;@property(copy,nonatomic)NSString *roomStyleName;//房間實體headRightModel的數組@property(strong,nonatomic)NSMutableArray *roomArray;@end

1.2房間實體的創建

#import <Foundation/Foundation.h>@interface headRightModel : NSObject@property(assign,nonatomic)long roomID;@property(copy,nonatomic)NSString *roomName;@property(copy,nonatomic)NSString *roomImageUrl;@end

二:單元格的創建

#import <UIKit/UIKit.h>#import "headRightModel.h"@interface rightCollectionViewCell : UICollectionViewCell@property(strong,nonatomic)headRightModel *curHeadRightModel;+(CGSize)ccellSize;@end
#import "rightCollectionViewCell.h"@interface rightCollectionViewCell()@property(strong,nonatomic)UIImageView *roomImageView;@property(strong,nonatomic)UILabel *roomLabel;@endstatic const CGFloat collectionCellHeight=80;static const CGFloat labelHeight=20;@implementation rightCollectionViewCell//這邊很關鍵 CollectionViewCell重用- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        if (self.roomImageView==nil) {            self.roomImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ([UIScreen mainScreen].bounds.size.width-80-10*3)/3, collectionCellHeight-labelHeight)];            self.roomImageView.contentMode=UIViewContentModeScaleaspectFill;            self.roomImageView.clipsToBounds = YES;            self.roomImageView.layer.masksToBounds = YES;            self.roomImageView.layer.cornerRadius = 2.0;            [self.contentView addSubview:self.roomImageView];        }                if (self.roomLabel==nil) {            self.roomLabel=[[UILabel alloc]init];            self.roomLabel.font=[UIFont systemFontOfSize:15];            self.roomLabel.textAlignment=NSTextAlignmentCenter;            [self.roomLabel sizeToFit];            [self.contentView addSubview:self.roomLabel];            [self.roomLabel mas_makeConstraints:^(MASConstraintMaker *make) {                make.top.mas_equalTo(self.roomImageView.mas_bottom).with.offset(2);                make.centerX.mas_equalTo(self.roomImageView).with.offset(0);                make.height.mas_equalTo(labelHeight);            }];        }    }    return self;}-(void)setCurHeadRightModel:(headRightModel *)curHeadRightModel{    _curHeadRightModel=curHeadRightModel;    self.roomImageView.image=[UIImage imageNamed:_curHeadRightModel.roomImageUrl];    self.roomLabel.text=_curHeadRightModel.roomName;}+(CGSize)ccellSize{    return CGSizeMake(([UIScreen mainScreen].bounds.size.width-80-10*3)/3,collectionCellHeight);}@end

三:創建節點顯示視圖

#import <UIKit/UIKit.h>@interface myHeadView : UICollectionReusableView- (void) setLabelText:(NSString *)text;@end

注意它是繼承UICollectionReusableView

#import "myHeadView.h"@interface myHeadView()@property (strong, nonatomic) UILabel *label;@end@implementation myHeadView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self)    {        self.label = [[UILabel alloc] init];        //在這邊調整它的位置        self.label.frame=CGRectMake(0, 10, 250, 25);        self.label.font = [UIFont systemFontOfSize:18];        self.label.backgroundColor=[UIColor brownColor];        self.label.textColor=[UIColor yellowColor];        [self addSubview:self.label];    }    return self;}- (void) setLabelText:(NSString *)text{    self.label.text = text;}@end

四:創建測試數據跟初始化集合列表

- (void)viewDidLoad {    [super viewDidLoad];        //初始化    self.view.backgroundColor=[UIColor whiteColor];    self.dataList=[[NSMutableArray alloc]init];    self.rightdataList=[[NSMutableArray alloc]init];    self.allRightDataList=[[NSMutableArray alloc]init];    self.isReturnLastOffset=YES;    //是否允許右位保持滾動位置    self.isKeepScrollState=YES;    //測試數據    for (int i=0; i<10; i++) {                //左邊列表數據        leftTagModel *item=[[leftTagModel alloc]init];        item.tagID=i;        item.tagName=[NSString stringWithFormat:@"第%d層",i];        [self.dataList addObject:item];                //右邊列表數據        for (int j=0; j<10; j++) {            rightModel *model=[[rightModel alloc]init];            model.tagID=i;            model.roomStyleID=j;            model.roomStyleName=[NSString stringWithFormat:@"%d層類型%d",i,j];            NSMutableArray *headRightModelArray=[[NSMutableArray alloc]init];            for (int z=0; z<20; z++) {                headRightModel *headrightModel=[[headRightModel alloc]init];                headrightModel.roomID=z;                headrightModel.roomName=[NSString stringWithFormat:@"%d類房間%d",j,z];                headrightModel.roomImageUrl=[NSString stringWithFormat:@"room%d",z%5];                [headRightModelArray addObject:headrightModel];            }            model.roomArray=headRightModelArray;            [self.allRightDataList addObject:model];        }    }        //創建列表    if (!_myTableView) {        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,tableWidthSize, kScreenHeight) style:UITableViewStylePlain];        _myTableView.backgroundColor=[UIColor grayColor];        _myTableView.showsVerticalScrollIndicator = NO;        _myTableView.showsHorizontalScrollIndicator=NO;        _myTableView.dataSource = self;        _myTableView.delegate = self;        _myTableView.tableFooterView=[[UIView alloc]init];        _myTableView.separatorColor= [UIColor colorWithRed:52.0f/255.0f green:53.0f/255.0f blue:61.0f/255.0f alpha:1];        [_myTableView registerClass:[leftTableCell class] forCellReuseIdentifier:NSStringFromClass([leftTableCell class])];        if ([self.myTableView respondsToSelector:@selector(setLayoutMargins:)]) {            self.myTableView.layoutMargins=UIEdgeInsetsZero;        }        if ([self.myTableView respondsToSelector:@selector(setSeparatorInset:)]) {            self.myTableView.separatorInset=UIEdgeInsetsZero;        }        [self.view addSubview:_myTableView];    }        //創建集合表格    if (!_myCollectionView) {        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];        self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(tableWidthSize+leftMargin,64, kScreenWidth-tableWidthSize-2*leftMargin, kScreenHeight) collectionViewLayout:layout];        self.myCollectionView.backgroundColor=[UIColor whiteColor];        self.myCollectionView.showsHorizontalScrollIndicator=NO;        self.myCollectionView.showsVerticalScrollIndicator=NO;        [self.myCollectionView registerClass:[rightCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([rightCollectionViewCell class])];        [self.myCollectionView registerClass:[myHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([myHeadView class])];        self.myCollectionView.dataSource = self;        self.myCollectionView.delegate = self;        [self.view addSubview:self.myCollectionView];    }        self.selectIndex=0;    //默認選擇第一個    if (self.dataList.count>0) {        self.curSelectModel=[self.dataList objectAtIndex:self.selectIndex];        [self.myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];        [self.myTableView reloadData];                //右邊數據加載        [self predicateDataSoure];    }}

注意:關于節視圖的注冊

[self.myCollectionView registerClass:[myHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([myHeadView class])];

五:集合視圖UICollectionViewDataSource, UICollectionViewDelegate的內容

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    return self.rightdataList.count;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    rightModel * array=self.rightdataList[section];        if (array.roomArray.count==0) {        return 1;    }    else    {        return array.roomArray.count;    }}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    rightCollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([rightCollectionViewCell class]) forIndexPath:indexPath];    rightModel * array=self.rightdataList[indexPath.section];    headRightModel *model=[array.roomArray objectAtIndex:indexPath.row];    ccell.curHeadRightModel=model;    return ccell;}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{    CGSize size = {240,40};    return size;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{        myHeadView *headView;    rightModel * array=self.rightdataList[indexPath.section];    if([kind isEqual:UICollectionElementKindSectionHeader])    {        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([myHeadView class]) forIndexPath:indexPath];        //別在這對headView坐標做處理        [headView setLabelText:[NSString stringWithFormat:@"%@",array.roomStyleName]];    }        return headView;}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    return [rightCollectionViewCell ccellSize];}- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{    return UIEdgeInsetsZero;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{    return 5;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{    return 5;}- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}

注意:別在viewForSupplementaryElementOfKind,對myHeadView進行坐標的調整,因為它是全局的,會導致所有的節點都混在一起,記得設置它的節頭大小,才會顯示出來;

 

 六:擴展關于viewForSupplementaryElementOfKind,它可以設置節頭跟節腳,下面引用網上一個比較全的說明

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath為collection view添加一個補充視圖(頁眉或頁腳)- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section設定頁眉的尺寸- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section設定頁腳的尺寸- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier

視圖創建:

[self.myCollectionView registerClass:[MyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader"];[self.myCollectionView registerClass:[MyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwHeader"];-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{    CGSize size = {240,25};    return size;}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{    CGSize size = {240,25};    return size;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    MyHeadView *headView;        if([kind isEqual:UICollectionElementKindSectionHeader])    {         headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];        [headView setLabelText:[NSString stringWithFormat:@"section %d's header",indexPath.section]];    }    else if([kind isEqual:UICollectionElementKindSectionFooter])    {        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];        [headView setLabelText:[NSString stringWithFormat:@"section %d's footer",indexPath.section]];    }    return headView;}

 

 實例源代碼下載地址


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲午夜激情免费视频| 日本不卡免费高清视频| 亚洲理论电影网| 欧美激情一区二区三区在线视频观看| 亚洲美女激情视频| 国产视频自拍一区| 日韩电影中文字幕av| 国产日韩精品入口| 欧美日韩亚洲精品一区二区三区| 91免费欧美精品| 国产欧美精品一区二区三区介绍| 在线播放日韩专区| 亚洲视频国产视频| 国产成人精品999| 欧美综合激情网| 日韩亚洲精品电影| 欧美肥臀大乳一区二区免费视频| 日韩大片免费观看视频播放| 中日韩美女免费视频网址在线观看| 欧美成人第一页| 久久久久北条麻妃免费看| 这里只有视频精品| 成人性生交大片免费看视频直播| 欧美精品亚州精品| 亚洲人成自拍网站| 最近2019中文字幕第三页视频| 日韩小视频在线| 日韩免费av片在线观看| 91免费电影网站| 国产精品久久久av久久久| 91精品久久久久久久久久久久久久| 成人精品久久一区二区三区| 91成人国产在线观看| 亚洲xxxxx性| 97精品一区二区视频在线观看| 亚洲精品一区av在线播放| 一区二区三区视频观看| 色七七影院综合| 欧美午夜精品久久久久久浪潮| 这里只有精品视频| 欧美裸体xxxx极品少妇| 国产精品久久久久999| 成人黄色av网| 亚洲欧美在线一区二区| 久久成人精品一区二区三区| 欧美精品videofree1080p| 欧美激情国产高清| 亚洲综合视频1区| 成人在线观看视频网站| 亚洲精品98久久久久久中文字幕| 91av视频在线| 亚洲第一二三四五区| 成人黄色生活片| 国产精品久久久久不卡| 成人网在线免费观看| 国产aⅴ夜夜欢一区二区三区| 欧美亚洲第一页| 一二美女精品欧洲| 欧美视频在线观看免费| 亚洲精品久久视频| 青青草原一区二区| 欧美黑人国产人伦爽爽爽| 久久久久久久久电影| 久久伊人免费视频| 成人激情在线播放| 国产免费久久av| 欧美精品videosex性欧美| 国产中文字幕日韩| 欧美色欧美亚洲高清在线视频| 久久99精品国产99久久6尤物| 欧美第一淫aaasss性| 久久人人爽人人| 色综合视频网站| 欧美在线视频免费播放| 久久精品一区中文字幕| 欧美午夜激情在线| 7777精品视频| 精品动漫一区二区三区| 91热精品视频| 欧美第一页在线| 亚洲欧洲午夜一线一品| 亚洲色图50p| 热门国产精品亚洲第一区在线| 91视频九色网站| 亚洲最新中文字幕| 亚洲午夜未满十八勿入免费观看全集| 黄色成人av网| 国产在线视频91| 国产精品久久久久久av福利软件| 欧美性极品xxxx娇小| 国内精久久久久久久久久人| 欧美在线观看网址综合| 国自在线精品视频| 91精品免费久久久久久久久| 亚洲欧洲日本专区| 久久91亚洲人成电影网站| 亚洲精品之草原avav久久| 亚洲影院色在线观看免费| 亚洲自拍在线观看| 国产精品久久久久av| 国产欧美va欧美va香蕉在| 国产欧美在线播放| 97av在线视频| 国产精品入口日韩视频大尺度| 黑丝美女久久久| 精品亚洲夜色av98在线观看| 欧美精品video| 久久天堂电影网| 亚洲一区二区日本| 日韩精品视频免费专区在线播放| 久久久999国产精品| 91在线视频一区| 97免费视频在线| zzjj国产精品一区二区| 久久综合色影院| 美女av一区二区| 欧美日韩亚洲激情| 亚洲男子天堂网| 国产精品久久久久一区二区| 久久夜色精品亚洲噜噜国产mv| 国产91露脸中文字幕在线| 国外成人在线播放| 国产精品久久久| 亚洲无限av看| 亚洲视频电影图片偷拍一区| 国产91精品久| 日韩精品在线观看一区二区| 一区二区三区视频观看| 这里只有精品视频在线| 欧美中文在线字幕| 亚洲欧美日韩另类| 日韩精品免费在线视频观看| 欧美日韩aaaa| 亚洲曰本av电影| 中文字幕精品在线视频| 欧美日韩国内自拍| www.亚洲人.com| 国产成人小视频在线观看| 日韩激情第一页| 国产精品免费一区豆花| 日韩精品在线播放| 亚洲精品成人久久电影| 国产伊人精品在线| 亚洲图中文字幕| 大量国产精品视频| 久久精品久久精品亚洲人| 91精品国产色综合久久不卡98| 欧美视频中文字幕在线| 久久电影一区二区| 欧美丰满少妇xxxxx做受| 国内成人精品一区| 国产999精品视频| 欧美国产日本在线| 欧美精品激情在线| 亚洲国产欧美精品| 亚洲精品福利免费在线观看| 性欧美暴力猛交69hd| 欧美激情亚洲激情| 草民午夜欧美限制a级福利片| 久久久久久国产精品美女| 亚洲免费电影一区| 性欧美亚洲xxxx乳在线观看| 日日噜噜噜夜夜爽亚洲精品| 国产盗摄xxxx视频xxx69|