美文网首页
[iOS]UITableView\UICollectionVie

[iOS]UITableView\UICollectionVie

作者: noikin | 来源:发表于2016-09-16 17:31 被阅读2829次

    多样式cell的重用机制

    学习IOS已经有差不多三个月时间了,第一篇文章,记录一下这几个月来我以及周围朋友初级阶段都会遇到的一个问题-----类似于图上的功能(一个UITableView或者UICollectionView 通过选择按钮切换不同的数据源、以及内容)
    这样的构造在许多应用都会用到,因此,我觉得这一个知识点很有必要掌握。很神奇的是,和我一起学习的童鞋们不会用的居然是占大多数。下面,我会大概来讲一讲它的使用步骤。(我的第一个APP 作为我的第一个demo,有些小紧张/(ㄒoㄒ)/~~屏幕没有适配、233)

    1.写UITableView/UICollectionView

    if(!_conllertionView){
          _conllertionView = [UICollectionView alloc]initWithFrame(0,self.frame.size.height/3.0,
    self.frame.size.width,self.frame.size.height - self.frame.size.height/3.0)];
         _conllertionView.delegata = self;
         _conllertionView.dataSource = self;
    }
          return _conllertionView;
    

    注意---
    代理写完后,记住加上协议
    UITableView:UITableViewDelegate / UITableViewDataSource
    UICollectionView:UICollectionViewDelegate / UICollectionViewDataSource

    随后加上他们必须要的方法,这里我以UICollectionView为例

    //Section个数
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    }
    //数据个数(cell个数)
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    }
    //cell内容
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    }
    //上下左右距离
    -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    }
    //每个UICollectionView大小
    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    }
    
    //设置cell行距
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    }
    

    基本的设置上大概完成了、以下内容是重点。首先分析我们点击的每一个按钮都会对应不同的界面。

    点击漫画、它的cell是圆形的 点击漫推,它的cell是方形的 点击动画,是长方形的
    然后,通过对界面的分析,我们可以知道,完成这样的效果是需要用到重用机制的。因此、我们需要用到不同的cell。所以,分别建立不同的基于(UICollectionViewCell的类,分别对其进行设置。(这里注意把数据需要使用的控件定义为属性放到.h文件里哦~)
    做到这里大部分的工作都已经完成了,最复杂,同时也是许多新手不容易想到的地方出现了。我们很容易想到这个效果会用不同的Cell,但是究竟如何使用却成为了一个难题。大多数新手会忘记“重用”,这里的重用不是简单的对相同样式进行重用,而是利用它的重用机制,完成不同样式的切换效果。
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.title1 isEqualToString:@"漫画"]) {        
    [_conllertionView registerClass:[CartCollectionViewCell class] forCellWithReuseIdentifier:@"CartCell"];    
        CartCollectionViewCell *myCell=[collectionView dequeueReusableCellWithReuseIdentifier:  @"CartCell" forIndexPath:indexPath];     
         //相关设置 
              self.nowCell = myCell;   
     }else if ([self.title1 isEqualToString:@"我的漫推"]){     
             [_conllertionView registerClass:[ManTuiCollectionViewCell class]forCellWithReuseIdentifier:@"MantuiCell"];   
             ManTuiCollectionViewCell *myCell1=[collectionView dequeueReusableCellWithReuseIdentifier:  @"MantuiCell" forIndexPath:indexPath];        
     //相关设置               
             self.nowCell = myCell1;   
     }else if([self.title1 isEqualToString:@"动画"]){  
          [_conllertionView registerClass:[AnimaCollectionViewCell class] forCellWithReuseIdentifier:@"AnimaCell"];    
          AnimaCollectionViewCell *myCell2=[collectionView dequeueReusableCellWithReuseIdentifier:@"AnimaCell" forIndexPath:indexPath]; 
     //相关设置      
           self.nowCell = myCell2;    } 
       return self.nowCell;   
    }
    

    我们利用它的重用机制,针对三次点击,注册了不同的cell,这样,就可以有我们想要达到的效果了。只有我们点击了不同的标签,程序就会找到自己所对应的cell进行设置,这步完成后,千万不要忘记每个UICollectionView的其他属性也会根据不同的标签进行改变的。
    (这里我以返回数量为例,其他属性设置方法都是雷同的)

    //数据个数(cell个数)
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
           if ([self.title1 isEqualToString:@"漫画"]) {        
               return "漫画数据源个数";
              }else if ([self.title1 isEqualToString:@"我的漫推"]){     
              return "我的漫推数据源个数";
                         }else if([self.title1 isEqualToString:@"动画"]){  
                          return "漫画数据源个数";   
                                } else{
                                            return 10;
                         }
    }
    

    总结
    1.创建不同数据来存放数据(这里我推荐使用MVC模式,将数据放进Mode里某些方面还能避免数据重用)
    2.创建不同Cell使用其样式
    3.数据源改变,UICollectionView的其他属性也会根据需求进行改变

    写到这里,大部分的东西也都讲完了,如果还有什么地方不明白,可以一起进行交流,探讨。

    最后,我的表述能力可能不是很好,如有哪些地方描述得不合适,请见谅。

    相关文章

      网友评论

          本文标题:[iOS]UITableView\UICollectionVie

          本文链接:https://www.haomeiwen.com/subject/tqwmettx.html