日韩性视频-久久久蜜桃-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的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 正在播放国产一区 | 91入囗| 国产男女猛烈无遮挡免费视频 | 中文字幕人妻一区二区三区视频 | 伊人精品视频 | 久久99精品久久久水蜜桃 | 蜜桃aaa| 1024手机看片日韩 | 国产av电影一区 | 69xxxx日本| 日韩免费视频网站 | 人成网站在线观看 | 草逼视频网 | 国产三级精品在线观看 | 狠狠干青青草 | 欧美极品少妇xxxxⅹ裸体艺术 | 污污的视频在线观看 | 色哟哟在线观看视频 | xxxx在线播放 | 国产精品久久久久久久久动漫 | 亚洲自拍偷拍av | 久久久午夜视频 | 日日夜夜国产精品 | 激情的网站 | 亚洲精品成人无码毛片 | 国产碰碰 | 亚洲av无码一区二区二三区 | 日本美女裸体视频 | 91porny在线 | 国产女上位 | 青青草福利| 久久久久久久久久久国产精品 | 一级特黄欧美 | 日本一区不卡视频 | 色播综合网 | 国产性猛交xx乱 | 午夜免费体验区 | 国产白浆在线观看 | 一级大片视频 | 免费看片黄色 | 国产1区二区 | 国产精品系列在线观看 | 国产chinese男男网站大全 | 夫妻啪啪呻吟x一88av | 久久免费视频精品 | 日韩电影福利 | 91久久精品www人人做人人爽 | 日本a在线天堂 | 亚洲av久久久噜噜噜熟女软件 | 高潮爽爆喷水h | a级黄色片网站 | youjizz中国少妇| 男生坤坤放进女生坤坤里 | 国产色呦呦 | 佐山爱av在线| 51av视频| 欧美亚洲91 | 特大巨交吊性xxxx | 亚洲色图狠狠干 | 蜜桃综合网 | 久久99婷婷 | 国产视频一区二区不卡 | 夜夜撸影院 | 免费精品无码AV片在线观看黄 | 亚洲激情视频在线 | 亚洲 小说 欧美 激情 另类 | 国产剧情在线视频 | 99久久久无码国产精品免费蜜柚 | 国产叼嘿视频 | 精品www久久久久久奶水 | 黄色一级视频片 | 娇妻第一次尝试交换的后果 | 谁有免费黄色网址 | 国产精品黄色大片 | 色偷偷av| 四虎永久免费观看 | 草草屁屁影院 | 精品人妻一区二区三 | 亚洲免费一 | 欧美一区二区在线观看视频 | 四色成人 | 亚洲欧美国产一区二区三区 | 波多野结衣一级 | 国产一区福利 | www.色午夜.com| 国产精品视频在线观看免费 | 91国产中文字幕 | 97在线视频免费观看 | 久久伊人热 | 日韩一区不卡视频 | 国产成人精品久久 | 亚洲成人黄色在线 | 不卡国产在线 | 夜夜骚av一区二区三区 | 婷婷在线免费观看 | 熟女俱乐部五十路六十路av | 国产亚洲欧洲 | 亚洲国产日韩一区二区 | 亚洲成人福利视频 |