【iOS學習筆記】用collectionView解決大量button的性能問題

原文可訪問:http://blog.csdn.net/u013883974/article/details/50130259xcode

 

在剛入門的階段,咱們的目標可能只是追求把界面碼出來,數據請求過來,並展現在界面上。app

因此也會忽視掉對效率的追求和優化,看完懶加載以後,發現本身的代碼裏面有不少地方都冗餘了。框架

在寫代碼的同時,要反覆問本身有沒有更簡單的,更高效的方法,抽空回顧本身已經寫的代碼,其實會發現,有不少地方須要更改,甚至整個框架都要修改。這就暴露了當初在設計的時候沒有理清關係的問題。函數

今天要寫的是collectionView,和tableView相比較,collectionView可能低調的不出名。可是collectionView能夠用在不少地方,起到點睛的效果。oop

拿上面的界面來講,大的結構確定是tableView沒話說,可是第一部分的兩行圖標,這裏怎麼作是個問題。當初我直接不假思索,來了一個暴力for循環,而後循環8次,把這個section給作出來了。優化

OK,command+r跑一下,發現是那麼回事,可是滑動的時候明顯略微有點卡頓。spa

當時覺得是tableView數據加載的時候形成的卡頓,想着後面優化一下就好。.net

今天想到了這個問題,而後想起以前在開發羣裏面問過別人相似的問題,也是多個button的問題,致使滑動很是卡頓。設計

因而用collectionView來替代暴力for循環創造button。代理

部分代碼以下,首先自定義collectionViewCell

 

 

[objc]  view plain copy
  1. #import "ActivityCollectionViewCell.h"  
  2.   
  3. @implementation ActivityCollectionViewCell  
  4.   
  5. - (id)initWithFrame:(CGRect)frame  
  6. {  
  7.     self = [super initWithFrame:frame];  
  8.     if (self)  
  9.     {  
  10.         [self addImgView];  
  11.     }  
  12.     return self;  
  13. }  
  14.   
  15. - (void)addImgView{  
  16.       
  17.     CGFloat width = 45;  
  18.       
  19.     _imgView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH/4.0-width)/2.0, 5, 45, 45)];  
  20.       
  21.     _titleLabel = [UILabel new];  
  22.     _titleLabel.textColor = [UIColor colorWithRed:139/255.0 green:139/255.0 blue:139/255.0 alpha:1.0];  
  23.     _titleLabel.textAlignment = NSTextAlignmentCenter;  
  24.     _titleLabel.adjustsFontSizeToFitWidth = YES;  
  25.     _titleLabel.font = [UIFont systemFontOfSize:13];  
  26.     _titleLabel.frame = CGRectMake((SCREEN_WIDTH/4.0-width)/2.0, _imgView.frame.size.height+_imgView.frame.origin.y+5, width, 20.0);  
  27.       
  28.     [self addSubview:_imgView];  
  29.       
  30.     [self addSubview:_titleLabel];  
  31. }  
  32.   
  33. @end  

 

 

而後在主界面添加一個collectionView的初始化函數,註冊collectionViewCell,設置cell大小:

 

 

[objc]  view plain copy
  1. - (void)initCollectionView{  
  2.       
  3.     titleArray = [[NSArray alloc]initWithObjects:@"跑步", @"羽毛球",@"游泳",@"騎行",@"乒乓球",@"健身",@"舞蹈",@"更多",nil];  
  4.     imageArray = [[NSArray alloc]initWithObjects:@"01paobu",@"02yumaoqiu",@"03youyong",@"04qixing",@"05pingpang",@"06jianshen",@"07wudao",@"15more",nil];  
  5.       
  6.     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];  
  7.     flowLayout.itemSize = CGSizeMake(40, 40);  
  8.     flowLayout.minimumLineSpacing = 0;  
  9.     flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//滑動方向  
  10.       
  11.     btnCollectionView.alwaysBounceHorizontal = YES;//控制水平方向遇到邊框是否反彈 BOOL   
  12.     btnCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 165) collectionViewLayout:flowLayout];  
  13.       
  14.     btnCollectionView.backgroundColor = [UIColor whiteColor];  
  15.     btnCollectionView.pagingEnabled = YES;  
  16.     btnCollectionView.showsHorizontalScrollIndicator = NO;  
  17.     btnCollectionView.showsVerticalScrollIndicator = NO;  
  18.     [btnCollectionView registerClass:[ActivityCollectionViewCell class] forCellWithReuseIdentifier:@"depresscell"];//註冊cell  
  19.     btnCollectionView.delegate = self;  
  20.     btnCollectionView.dataSource = self;  
  21. }  

 

 

而後實現collectionView的代理方法

 

 

[objc]  view plain copy
  1. #pragma mark -- UICollectionViewDataSource  
  2. //定義展現的UICollectionViewCell的個數  
  3. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
  4. {  
  5.     return 8;  
  6. }  
  7. //定義展現的Section的個數  
  8. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView  
  9. {  
  10.     return 1;  
  11. }  
  12. //每一個UICollectionView展現的內容  
  13. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
  14. {  
  15.     static NSString * CellIdentifier = @"depresscell";  
  16.       
  17.     //重用cell  
  18.     ActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];  
  19.       
  20.     cell.imgView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];  
  21.     cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];  
  22.       
  23.     return cell;  
  24. }  
  25.   
  26. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{  
  27.     CGSize size={0,0};  
  28.     return size;  
  29. }  
  30.   
  31. #pragma mark --UICollectionViewDelegateFlowLayout  
  32. //定義每一個UICollectionView 的大小  
  33. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath  
  34. {  
  35.     return CGSizeMake(SCREEN_WIDTH/4.0, 80);  
  36. }  
  37. //定義每一個UICollectionView 的 margin  
  38. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
  39. {  
  40.     return UIEdgeInsetsMake(2.5, 0, 0, 0);  
  41. }  
  42.   
  43. // 定義上下cell的最小間距  
  44. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {  
  45.     return 0;  
  46. }  
  47.   
  48. // 定義左右cell的最小間距  
  49. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {  
  50.     return 0;  
  51. }  


最後定義collectionViewCell的點擊事件,來代理button的點擊事件,就能夠了

 

 

[objc]  view plain copy
  1. #pragma mark --UICollectionViewDelegate  
  2. //UICollectionView被選中時調用的方法  
  3. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5.     [self btnclicked:indexPath.row];//代替button的點擊事件  
  6. }  
  7. //返回這個UICollectionView是否能夠被選擇  
  8. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath  
  9. {  
  10.     return YES;  
  11. }  

寫完以後再run一下,發現卡頓問題解決了,collectionView是個神奇的東西,還有瀑布流,自定義標籤等等功能。

相關文章
相關標籤/搜索