iOS tableView、collectionView添加Se

作者: SamChenzx | 来源:发表于2016-12-26 21:21 被阅读1358次

          用过UITableView和UICollectionView的小伙伴都知道这两个view的继承自UIScrollView,因此也有着相类似的ViewDelegate和ViewDataSource,为了实现单元格Cell内容的个性话,我们可以自定义一个Cell类,然后在控制器中注册nib:

    [self.tableView registerNib [UINib nibWithNibName: @"tableViewCell" bundle: nil] forCellReuseIdentifier: identifier ];

    [self.collectionView registerNib: [UINib nibWithNibName:@"collectionViewCell" bundle: nil] forCellWithReuseIdentifier: identifier];

    返回indexPath对应Cell可以用:

    -(UITableViewCell*)tableView: (UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {...}

    -(UICollectionViewCell*)collectionView: (UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {...}

            不过当我们要设计对应的HeaderView的时候,两者在实现上体现出了稍微大点的区别,先看tableView,当我们自定义一个view之后,就可以把view给对应的section:

    - (UIView*)tableView:(UITableView*) tableView viewForHeaderInSection:(NSInteger) section {

    if(section ==0) {

       return self.infoView;

    }

       return nil;

    }

    然而collectionView中并没有viewForHeaderInSection方法,collectionView设置headerView, footerView的方法名略有特殊,是通过返回Supplementary element的View实现的。需要两个输入参数,一个是View的类型,UICollectionElementKindSectionHeader或UICollectionElementKindSectionFooter;另一个是所在的indexPath:

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

    {

    UICollectionReusableView *reusableView =nil;

    if(kind ==UICollectionElementKindSectionHeader){

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerView" forIndexPath: indexPath];

    reusableView = headerView;

    }

    if(kind ==UICollectionElementKindSectionFooter) {

    UICollectionReusableView *headerView =  [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerView" forIndexPath: indexPath];

    reusableView = headerView;

    }

    return reusableView;

    }

    这里需要注意的有以下几点:

    1. 如果自定义需要返回的headerView或footerView,不能继承自UIView,需要继承自UICollectionReusableView,并且一定需要返回非nil的reusableView,不然会报错。

    所以建议都去判断kind为headerView或footerView时,都给reusableView赋非nil的reusableView。

    2. 可以为自定义的UICollectionReusableView子类创建xib文件,并采用Collection Reusable View控件来布局,如果采用这种方式,一定需要在初始化UI的时候,注册该xib文件:

    [self.collectionView registerNib:[UINib nibWithNibName: @"headerView" bundle: nil] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerView"];

    不然,collectionView仍然会添加headerView,不过却不会加载xib中的元素以及约束条件,看到的是一块空白区域,有兴趣可以试一下。

    3. 如果仅仅是创建了继承自UICollectionReusableView的子类,但没有创建xib文件,而用代码添加元素及约束,一定要在初始化UI的时候,注册该子类:

    [self.collectionView registerClass:[HeadView class]  forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: identifier];

    不然会报错以下错误:

    注册xib文件或者class可以和注册Cell的代码放在一起,例如viewDidLoad中。

    4. 指定添加的headerView或footerView的大小可以用:

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

    CGSizesize={[UIScreen mainScreen].bounds.size.width,45};

    return size;

    }

    注意以上几点,应该能够正确的添加collectionView的headerView或footerView了,有问题可以交流。没有单独的collectionView的Demo,包含在一个稍微完整的Demo中,链接在这里

    相关文章

      网友评论

      • Bearger:代码应该是markdown代码块啊~怎么用引用包裹~!
        SamChenzx:@Bearger 第一次用简书,当时不知道markdown,以为引用有点像,谢谢提醒,我稍微调整下 :grin:

      本文标题:iOS tableView、collectionView添加Se

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