复习知识点:UITableView和UICollectionView的经常使用属性

UITableViewide

UICollectionView布局

  //UICollectionViewLayoutspa

    //UICollectionViewLayout决定了UICollectionView如何显示在界面上,Apple提供了一个最简单的默认layout对象:UICollectionViewFlowLayout对象

    //Flow Layout是一个Cells的线性布局方案,并具备页面和页脚。其可定制的内容以下:blog

    //itemSize属性事件

    //设定全局的Cell尺寸,若是想要单独定义某个Cell的尺寸,能够使用下面方法:ci

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;element

    

    //minimumLineSpacing属性it

    //设定全局的行间距,若是想要设定指定区内Cell的最小行距,能够使用下面方法:io

    

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

    

    //minimumInteritemSpacing属性

    //设定全局的Cell间距,若是想要设定指定区内Cell的最小间距,能够使用下面方法:

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

 

    //scrollDirection属性

    //设定滚动方向,有UICollectionViewScrollDirectionVerticalUICollectionViewScrollDirectionHorizontal两个值。

    //headerReferenceSize属性与footerReferenceSize属性

    //设定页眉和页脚的全局尺寸,须要注意的是,根据滚动方向不一样,headerfooterwidthheight中只有一个会起做用。若是要单独设置指定区内的页面和页脚尺寸,能够使用下面方法:

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

    

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

    

    //sectionInset属性

    //设定全局的区内边距,若是想要设定指定区的内边距,能够使用下面方法:

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

    

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

    //UICollectionViewDataSource

    //返回collection view里区(section)的个数,若是没有实现该方法,将默认返回1

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

    

    //返回指定区(section)包含的数据源条目数(number of items),该方法必须实现:

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

 

    //返回某个indexPath对应的cell,该方法必须实现:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    {

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

        if(indexPath.section==0)

        {

            cell.backgroundColor = [UIColor redColor];

        }

        else if(indexPath.section==1)

        {

            cell.backgroundColor = [UIColor greenColor];

        }

        return cell;

    }

    

    //UICollectionViewCell结构上相对比较简单,由下至上:

    //

    //首先是cell自己做为容器view

    //而后是一个大小自动适应整个cellbackgroundView,用做cell平时的背景

    //再其次是selectedBackgroundView,是cell被选中时的背景

    //最后是一个contentView,自定义内容应被加在这个view

    //collection view添加一个补充视图(页眉或页脚)

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

    

    //设定页眉的尺寸

    - (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

 

    //设定指定区内Cell的最小行距,也能够直接设置UICollectionViewFlowLayoutminimumLineSpacing属性

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

    

    //设定指定区内Cell的最小间距,也能够直接设置UICollectionViewFlowLayoutminimumInteritemSpacing属性

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

    

    //UICollectionViewDelegate

    //当指定indexPath处的item被选择时触发

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

 

    //P.s. 当你删除或添加元素时,必定要更新numberOfItemsInSection的返回状况。

    //当指定indexPath处的item被取消选择时触发,仅在容许多选时被调用

    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

 

    //下面是三个和高亮有关的方法:

    //事件的处理顺序以下:

    //

    //手指按下

    //shouldHighlightItemAtIndexPath (若是返回YES则向下执行,不然执行到这里为止)

    //didHighlightItemAtIndexPath (高亮)

    //手指松开

    //didUnhighlightItemAtIndexPath (取消高亮)

    //shouldSelectItemAtIndexPath (若是返回YES则向下执行,不然执行到这里为止)

    //didSelectItemAtIndexPath (执行选择事件)

    //若是只是简单实现点击后cell改变显示状态,只须要在cellForItemAtIndexPath方法里返回cell时,指定cellselectedBackgroundView

    - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

    - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath

    - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath

相关文章
相关标签/搜索