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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

iOS UICollectionView的实现

發布時間:2025/4/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS UICollectionView的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ios的UICollectionView并不能在iOS6之前的版本中使用,為了兼容之前的版本需要自定義UICollectionView。寫完之后發現人家已經有開源了,下過來看了看發現我是用UIScrollerView的委托真是多此一舉,完全可以用layout來實現嘛。我在判斷重用的時候用了一大堆if沒有別人寫的簡潔明了。 首先是定義委托,需要用戶傳入collection總item的總數與每一行中item的個數。其余的與UITableView的委托基本一致。 isNeedRefreshOrMore方法用來判斷用戶使用需要下拉刷新上拉更多的功能,返回YES就使用。 doCollectionRefresh即為響應下拉刷新事件。更多同樣。。 1 #pragma mark - 2 #pragma mark 委托 3 @protocol CustomCollectionDataSource<NSObject> 4 @required 5 //item的總個數 6 -(NSInteger)numberOfItemsInCollection; 7 //每一行的個數 8 -(NSInteger)numberOfItemsInRow; 9 @end 10 11 @protocol CustomCollectionDelegate<NSObject> 12 @required 13 -(CustomCollectionItem *)itemInCollectionAtPoint:(CGPoint)point collectionView:(CustomCollectionView *)collection; 14 @optional 15 -(void)itemDidSelectedAtPoint:(CGPoint)point; 16 -(BOOL)isNeedRefreshOrMore; 17 -(void)doCollectionRefresh; 18 -(void)doCollectionMore; 19 20 -(UIView *)collectionViewForHeader; 21 @end 在同文件中定義了頁面狀態的枚舉,用來區分Collcetion的狀態。同時定義了一些public方法 #import <UIKit/UIKit.h>typedef enum {CS_Init,CS_More,CS_Refresh }CollectionState;@class CustomCollectionItem; @protocol CustomCollectionDataSource; @protocol CustomCollectionDelegate;@interface CustomCollectionView : UIScrollView<UIScrollViewDelegate> @property (weak, nonatomic) id<CustomCollectionDataSource> dataSource; @property (weak, nonatomic) id<CustomCollectionDelegate> customDelegate;-(CustomCollectionItem *)dequeueReusableItemWithIdentifier:(NSString *)identifier; -(void)addItemsIntoDic; -(void)itemClickedAtPoint:(CGPoint)point; -(void)reloadData; -(CustomCollectionItem *)getItemAtPoint:(CGPoint)point;@property (strong,nonatomic) UIView *headerView; @end #import "CustomCollectionView.h" #import "CustomCollectionItem.h" #import "BaseRMView.h" @interface CustomCollectionView()
//重用池--原諒這個名字 @property (strong, nonatomic) NSMutableDictionary
*contentItemDictionary;
//能夠顯示的item數量(以行計) @property(assign,nonatomic) NSInteger showCount; @property (assign,nonatomic) NSInteger offRowIndex;
//分割線--沒有用到 默認成了10px @property (assign,nonatomic) CGFloat itemSpliteWidth;
//總行數 @property (assign,nonatomic) NSInteger rows; //item個數 @property (assign, nonatomic) NSInteger numberOfItemsInCollection;
//每行item個數 @property (assign,nonatomic) NSInteger numberOfItemsInRow;
//每一行的高度 @property (assign, nonatomic) CGFloat heightOfRow; // @property (assign, nonatomic) CGRect viewFrame; //是否第一次加載 @property (assign,nonatomic) BOOL isFirstLoad;
//上一次scrollview的offsetY,用來判斷是向上滑動還是向下滑動 @property (assign,nonatomic) CGFloat lastOffsetY;
//當前最后一行的index,從0開始 @property (assign,nonatomic) NSInteger lastRowIndex;
//當前最上一行的index,從0開始 @property (assign,nonatomic) NSInteger topRowIndex; //沒用 @property (assign,nonatomic) NSInteger numberOfMore; //是否需要顯示刷新更多頁面標志 @property (assign,nonatomic) BOOL isNeedShowMoreTag;
//刷新view @property (strong,nonatomic) BaseRMView
*refreshView;
//更多view @property (strong,nonatomic) BaseRMView
*moreView;@property (assign,nonatomic) CGFloat baseOffsetY; @property (assign,nonatomic) CGFloat baseCanMove; //reload之前的行數,上拉更多的時候如果用戶滑動的距離超過行高會出錯,用beforeRowCount來比較rows來判斷新增的item需要添加的坐標 @property (assign,nonatomic) NSInteger beforeRowCount;//@property (assign,nonatomic) NSInteger firstShowCount; @end #pragma mark - #pragma mark 頁面初始化 -(id)init{CGRect frame=[UIScreen mainScreen].applicationFrame;self=[self initWithFrame:frame];if(self){}return self; }- (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {_viewFrame=frame;self.delegate=self;_isFirstLoad=YES;_contentItemDictionary=[[NSMutableDictionary alloc] init];_isNeedShowMoreTag=NO;}return self; } #pragma mark - #pragma mark 數據初始化 -(void)loadData{if ([_dataSource respondsToSelector:@selector(numberOfItemsInCollection)]) {_numberOfItemsInCollection=[_dataSource numberOfItemsInCollection];}else{_numberOfItemsInCollection=0;}if([_dataSource respondsToSelector:@selector(numberOfItemsInRow)]){_numberOfItemsInRow=[_dataSource numberOfItemsInRow];_heightOfRow=((300.0-(_numberOfItemsInRow-1)*10)/_numberOfItemsInRow);_itemSpliteWidth=10;}else{_numberOfItemsInRow=3;//默認為3_heightOfRow=88;_itemSpliteWidth=18;}if ([_dataSource respondsToSelector:@selector(numberofMore)]) {_numberOfMore=[_dataSource numberofMore];}if ([_customDelegate respondsToSelector:@selector(isNeedRefreshOrMore)]) {_isNeedShowMoreTag=[_customDelegate isNeedRefreshOrMore];}if ([_customDelegate respondsToSelector:@selector(collectionViewForHeader)]) {_headerView=[_customDelegate collectionViewForHeader];if (![self.subviews containsObject:_headerView]) {[self addSubview:_headerView];}}//計算行數_rows=ceil((float)_numberOfItemsInCollection/_numberOfItemsInRow);CGFloat contentHeight=(_rows*_heightOfRow + (_rows+1)*10+_headerView.frame.size.height);CGFloat scrollContentHeight=contentHeight>_viewFrame.size.height?contentHeight:_viewFrame.size.height;//計算一頁能顯示多少行_showCount= (NSInteger)ceil((self.frame.size.height/(_heightOfRow+10)));[self setContentSize:CGSizeMake(320, scrollContentHeight)];//判斷是否有新增行,如果有當前最上義行index+1if (_rows!=_beforeRowCount&&_beforeRowCount!=0) {_topRowIndex++;}
  //從當前最上一行開始增加showcount行的item
for (int i=_topRowIndex; i<_topRowIndex+_showCount; i++) {[self creatItem:i];}if (_isNeedShowMoreTag==YES) {if (![self.subviews containsObject:_refreshView]) {_refreshView=[[BaseRMView alloc] initWithState:Refresh];[_refreshView setFrame:CGRectMake(0, -50, 320, 50)];[_refreshView setBackgroundColor:[UIColor grayColor]];[self addSubview:_refreshView];
} if (![self.subviews containsObject:_moreView]) {_moreView=[[BaseRMView alloc] initWithState:More];[_moreView setFrame:CGRectMake(0, self.contentSize.height, 320, 50)];[_moreView setBackgroundColor:[UIColor grayColor]];[self addSubview:_moreView];}else{[_moreView setFrame:CGRectMake(0, self.contentSize.height, 320, 50)];}} }

?

-(void)layoutSubviews{
//第一次加載時初始化數據,之后不需要重新計算
if (_isFirstLoad) {[self loadData];//offsetY基數 只在第一次移動時候,10為默認的分割線高度_baseOffsetY=(10*(_showCount+1)+_heightOfRow*_showCount)-self.frame.size.height;//移動基數_baseCanMove=10+_heightOfRow;_isFirstLoad=NO;_lastRowIndex=_showCount-1;_topRowIndex=0;} } //重新加載數據,記錄加載前的行數
-(void)reloadData{_beforeRowCount=_rows;[self loadData]; } #pragma mark - #pragma mark Item相關 -(void)creatItem:(NSInteger)rowIndex{if ([_customDelegate respondsToSelector:@selector(itemInCollectionAtPoint:collectionView:)]) {for (int j=0; j<_numberOfItemsInRow; j++) {//判斷當前個數是否超過了總個數(單數情況下)if (!(((rowIndex)*_numberOfItemsInRow+j+1)>_numberOfItemsInCollection)) {//根據委托創建itemCustomCollectionItem *item=[_customDelegate itemInCollectionAtPoint:CGPointMake(rowIndex, j) collectionView:self];//設置item的大小[item setFrame:CGRectMake(10+_heightOfRow*j+_itemSpliteWidth*j, 10+_heightOfRow*rowIndex+10*rowIndex+_headerView.frame.size.height, _heightOfRow, _heightOfRow)];//設置item的point坐標item.point=CGPointMake(rowIndex, j);//在view中加入item [self addSubview:item];}}} } //根據重用標志(reuseidentifier)從重用池中獲取item -(CustomCollectionItem *)dequeueReusableItemWithIdentifier:(NSString *)identifier{NSArray *cellArray=[self.contentItemDictionary objectForKey:identifier];if (cellArray.count==0) {return nil;}else{id firstObject=[cellArray objectAtIndex:0];if([firstObject isKindOfClass:[CustomCollectionItem class]]){//獲取item后從重用池中刪除item;CustomCollectionItem *item=firstObject;[[self.contentItemDictionary objectForKey:identifier] removeObject:firstObject];[item reset];return item;}else{return nil;}} } //根據point坐標從當前item數組中獲取item -(CustomCollectionItem *)getItemAtPoint:(CGPoint)point{CustomCollectionItem *result=nil;for (id item in self.subviews) {if ([item isKindOfClass:[CustomCollectionItem class]]) {if (((CustomCollectionItem *)item).point.x==point.x&& ((CustomCollectionItem *)item).point.y==point.y) {result=item;}}}return result; } -(void)addItemToPool:(CustomCollectionItem *)item{if([[self.contentItemDictionary allKeys] containsObject:item.reuseIdentifier]){[[self.contentItemDictionary objectForKey:item.reuseIdentifier] addObject:item];}else{NSMutableArray *cellArray=[NSMutableArray arrayWithObject:item];[self.contentItemDictionary setObject:cellArray forKey:item.reuseIdentifier];} } #pragma mark - #pragma mark 頁面滾動 //topRowIndex ---> 當前最上一行的index(從0開始); //lastRowIndex ---> 當前最后一行的index //removeIndex ---> 當前被移除的最后一行的行數(從1開始) //addIndex ---> 在showcount基礎上增加的行數 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{@try {//手指向上移動移動基數后將顯示下一頁面//手指向下移動移動基數將移除最下一行BOOL isMoveUp=TRUE;//是否向下滑if (scrollView.contentOffset.y-_lastOffsetY>0) {isMoveUp=FALSE;}else{isMoveUp=TRUE;}_lastOffsetY=scrollView.contentOffset.y;//刷新更多if (scrollView.contentOffset.y==0) {if ([self.subviews containsObject:_refreshView]) {[_refreshView changeState:Refresh];}}else if(scrollView.contentOffset.y==scrollView.contentSize.height-scrollView.frame.size.height){if ([self.subviews containsObject:_moreView]) {[_moreView changeState:More];}}else if (scrollView.contentOffset.y>(scrollView.contentSize.height-scrollView.frame.size.height) ||scrollView.contentOffset.y<0) {if (scrollView.contentOffset.y>=(scrollView.contentSize.height-scrollView.frame.size.height+50)) {if ([self.subviews containsObject:_moreView]&&_moreView.viewState==More) {[_moreView changeState:ToMore];}}else if (scrollView.contentOffset.y<-50){if ([self.subviews containsObject:_refreshView]&&_refreshView.viewState==Refresh) {[_refreshView changeState:ToRefresh];}}}else{//判斷重用if (scrollView.contentOffset.y>_headerView.frame.size.height) {CGFloat realMove=scrollView.contentOffset.y-_headerView.frame.size.height;//增加的row坐標 初始為0 移動一個移動基數后加/減1NSInteger addIndex=ceil((realMove-_baseOffsetY)/_baseCanMove);//刪除的row坐標 初始為0 移動一個移動基數后加/減1NSInteger removeIndex=(realMove/_baseCanMove);//手指向上移動if (!isMoveUp) {//如果最后一行編號==增加的row坐標+1&&增加的row坐標<總行數-1if (_lastRowIndex==addIndex+_showCount-2&&addIndex<_rows-1) {//最后一行坐標++_lastRowIndex++;//如果最后一行坐標!=總行數;如果相等則為最后一行不需要增加if (_lastRowIndex!=_rows) {[self creatItem:_lastRowIndex];}}//如果刪除的row坐標!=0&&刪除的row坐標!=最上一行坐標&&最上一行坐標<總行數-顯示行數if (removeIndex!=0&&removeIndex!=_topRowIndex&&_topRowIndex<_rows-_showCount) {for (int i=0; i<_numberOfItemsInRow; i++) {CustomCollectionItem *item=[self getItemAtPoint:CGPointMake(removeIndex-1, i)];if (item!=nil) {[self addItemToPool:item];[item removeFromSuperview];}}_topRowIndex++;}}else{//remove-->add add-->removeif (removeIndex==_topRowIndex-1) {[self creatItem:removeIndex];_topRowIndex--;}if (addIndex!=0&&addIndex!=_lastRowIndex-_showCount+1) {if (_lastRowIndex==_rows) {_lastRowIndex--;}else{for (int i=0; i<_numberOfItemsInRow; i++) {CustomCollectionItem *item=[self getItemAtPoint:CGPointMake(_lastRowIndex, i)];if (item!=nil) {[self addItemToPool:item];[item removeFromSuperview];}}_lastRowIndex--;}}}}}}@catch (NSException *exception) {NSLog(@"customCollectionView exception %@",exception.reason);} } #pragma mark- #pragma mark item點擊 -(void)itemClickedAtPoint:(CGPoint)point{if ([_customDelegate respondsToSelector:@selector(itemDidSelectedAtPoint:)]) {[_customDelegate itemDidSelectedAtPoint:point];} } #pragma mark- #pragma mark 刷新更多-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{if (scrollView.contentOffset.y<-50) {if (_isNeedShowMoreTag==YES&&[self.subviews containsObject:_refreshView]) {if ([_customDelegate respondsToSelector:@selector(doCollectionRefresh)]) {[_customDelegate doCollectionRefresh];}[_refreshView changeState:EndRefresh];}}else if (scrollView.contentOffset.y>scrollView.contentSize.height-scrollView.frame.size.height+50){if (_isNeedShowMoreTag==YES&&[self.subviews containsObject:_moreView]) {if ([_customDelegate respondsToSelector:@selector(doCollectionMore)]) {[_customDelegate doCollectionMore];}[_moreView changeState:EndMore];}} } #import <UIKit/UIKit.h> #import <objc/runtime.h> #import <Foundation/Foundation.h> @interface CustomCollectionItem : UIView<UIGestureRecognizerDelegate,NSCoding> @property (strong,nonatomic) UIImageView *backgroundImage; @property (strong,nonatomic) NSString *reuseIdentifier; @property (assign,nonatomic) CGPoint point; -(id)initWithReuseIdentifier:(NSString *)identifier; -(void)itemTaped; -(void)reset; @end #import "CustomCollectionItem.h" #import "CustomCollectionView.h"@interface CustomCollectionItem() @property(strong,nonatomic) UIView *contentView; @end@implementation CustomCollectionItem-(id)initWithReuseIdentifier:(NSString *)identifier{self=[super init];if (self) {_reuseIdentifier=identifier;[self setUserInteractionEnabled:YES];_backgroundImage= [[UIImageView alloc] init];}return self; }-(void)setFrame:(CGRect)frame {[super setFrame:frame];[_backgroundImage setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];_backgroundImage.tag=10099; }-(void)layoutSubviews {[super layoutSubviews];if([self viewWithTag:10099]== nil) {[self addSubview:_backgroundImage];[self sendSubviewToBack:_backgroundImage];}UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTaped)];[self addGestureRecognizer:tapGR]; }-(void)itemTaped{[(CustomCollectionView *)[self superview] itemClickedAtPoint:self.point]; }-(void)setBackgroundImage:(UIImageView *)backgroundImage {_backgroundImage=backgroundImage; }#pragma override -(void)reset{}- (void)encodeWithCoder:(NSCoder*)coder {Class clazz = [self class];u_int count;objc_property_t* properties = class_copyPropertyList(clazz, &count);NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];for (int i = 0; i < count ; i++){const char* propertyName = property_getName(properties[i]);[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];}free(properties);for (NSString *name in propertyArray){id value = [self valueForKey:name];[coder encodeObject:value forKey:name];} }- (id)initWithCoder:(NSCoder*)decoder {if (self = [super init]){if (decoder == nil){return self;}Class clazz = [self class];u_int count;objc_property_t* properties = class_copyPropertyList(clazz, &count);NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];for (int i = 0; i < count ; i++){const char* propertyName = property_getName(properties[i]);[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];}free(properties);for (NSString *name in propertyArray){id value = [decoder decodeObjectForKey:name];[self setValue:value forKey:name];}}return self; } @end

?

轉載于:https://www.cnblogs.com/independent/p/3205419.html

總結

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

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

主站蜘蛛池模板: 深夜福利一区二区三区 | 二级黄色录像 | 国产大屁股喷水视频在线观看 | av网站免费在线播放 | 寂寞午夜影院 | 美女扒开尿口给男人桶 | 91免费看视频 | 台湾佬综合网 | 青青草在线免费观看 | 2017天天干 | 日韩一区久久 | 日韩精品免费一区二区三区竹菊 | 九九久久99 | 无码aⅴ精品一区二区三区 精品久久在线 | 亚洲国产精品综合 | 91精选 | 久久久老熟女一区二区三区91 | 操操影视 | 国产亚洲制服欧洲高清一区 | 久久久男人的天堂 | 亚洲精品视屏 | 97久久国产 | 欧美日韩综合一区 | 网站久久 | 98色| 国产日韩欧美综合在线 | 国产午夜精品久久久久久久久久 | 黄色大片在线播放 | 精品一级少妇久久久久久久 | www.欧美色图 | 在线99视频 | 色婷婷av777 麻豆传媒网站 | 人妻天天爽夜夜爽一区二区三区 | 午夜精产品一区二区在线观看的 | 国产黄色小视频在线观看 | 国产精品久久久久久久久久辛辛 | 爱爱网视频 | 黄色三级图片 | aaa国产视频 | 色欧美综合 | 免费在线观看中文字幕 | аⅴ资源中文在线天堂 | 欧美日本色 | 国产精品视频www | 成人黄页 | 日本免费黄视频 | 96国产视频 | 全程偷拍露脸中年夫妇 | 91亚洲精品在线观看 | 欧美.com | 亚洲av无码一区二区三区性色 | 亚洲免费视频播放 | 国产精品亚洲一区二区三区 | 极品videosvideo喷水 | 99自拍视频在线观看 | 桃色视频网站 | 午夜影视免费 | 色88久久久久高潮综合影院 | 日本免费一区二区三区最新 | 日一日射一射 | 日韩精品免费视频 | 国产农村熟妇videos | 98成人网| 精品久久久无码中文字幕 | 三级做爰第一次 | 成人在线观看www | 久久久亚洲一区二区三区 | 亚洲看 | 欧美色悠悠 | 中日韩精品一区二区三区 | 久久高清免费 | av中文在线播放 | 欧美亚洲精品一区二区 | 少妇淫片| 一区二区三区在线免费视频 | 亚洲综合久久网 | 毛片aaa| 农村少妇久久久久久久 | 91精品综合久久久久久五月天 | 成年人视频在线免费观看 | 综合色爱| 精品久久久久久久无码 | 亚洲一区欧洲二区 | 欧美色图第一页 | 亚洲自拍小视频 | 中文字幕四区 | 午夜成人鲁丝片午夜精品 | 草碰在线 | 精品黑人一区二区三区国语馆 | 四虎永久在线精品免费网址 | 久久国产精品一区 | 雷电将军和丘丘人繁衍后代视频 | 亚洲蜜臀av乱码久久精品蜜桃 | av国产免费 | 无码熟妇人妻av | 国产日韩欧美在线 | 亚洲第3页 | 日本欧美中文字幕 | 欧美成人a交片免费看 |