iOS UICollectionViewCell一些小坑

来源:互联网 发布:淘宝账号查别人的信息 编辑:程序博客网 时间:2024/06/10 09:31

CollectionView相比较TableView而言,更加灵活,也更加强大。But一些坑还是挺多的,例如:

  • Section的背景色,至今没有搞定,不过在 http://stackoverflow.com/ 上面有解决的办法,时间长了,忘了链接了,要是有兴趣,可以一起讨论。

  • 其次关于header的问题,由于collectionview没有像tableview一样的header,所以我选择用第一个section的header当作整个collectionview的header,这里坑就大了,当时做基于一个群成员的需求,上面有一个搜索框,红色部分。如图:

  • 这里写图片描述
    当我手指向上滑动之后,点击头像,进入个人信息页面,然后回退,这个时候诡异的事情发生了,我的 搜索框不见了!!!不见了!!!更诡异的事情在下面,当我用Xcode查看视图时候,发现searchBar还在!!!然而当我再次点击搜索空白处的时候,searchBar又显示出来的,但是通过查看视图时候,发现searchBar这个对象没了!!!没了!!!没了!!!抛出问题,有解决办法的或者类似的可以私聊。最后为了完成这个功能,用tableview搞定了需求。

UICollectionViewCell

自己翻译

A UICollection​View​Cell object presents the content for a single data item when that item is within the collection view’s visible bounds. You can use this class as-is or subclass it to add additional properties and methods. The layout and presentation of cells is managed by the collection view and its corresponding layout object.

划重点

通常使用cell的时候,通常会按照这样的顺序来做。

  • 注册Cell
[_collectionView registerClass:classType                           forCellWithReuseIdentifier:identifier];
  • 其次,我们会在下面这个方法写上这样类似的代码:
//每个要显示的cell- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    //根据identifier从缓冲池里去出cell    DemoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];    return cell;}

这样的写法是完完全全的正确的。

在这里请对比tableview使用cell的不同地方。

  • 第一,collectioview要想使用cell,必须注册,而tableview不需要。
  • 第二,在cellforindex方法里面,tableview的方法是这样写的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];        //这里必须做判空处理,因为有可能cell没有在缓冲队列里面        if (!cell) {            cell = [[DemoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];        }        //这里采用setDataModel 为数组        return cell;    }}
  • 使用CollectionViewCell 之前必须注册
  • 其次如果队列中没有缓冲的cell,iOS会自动初始化你已经注册的cell
  • 最后,我不想复用,如果这样写会怎么样:
//每个要显示的cell- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    //根据identifier从缓冲池里去出cell    DemoCell * cell = [[DemoCell alloc] init];    return cell;}

不好意思,运行时候直接报错,因为UICollectionViewCell必须是从缓冲区取得的,并且,如果缓冲区没有,它会自动初始化,如果你没有注册cell,就不知道初始化cell的类型,他也会报错的。
看看官方的文档,加粗部分:
//强调必须从dequeue里面取,不能使用自己初始化的cell
Creating Cells and Supplementary Views
The collection view’s data source object provides both the content for items and the views used to present that content. When the collection view first loads its content, it asks its data source to provide a view for each visible item. To simplify the creation process for your code, the collection view requires that you always dequeue views, rather than create them explicitly in your code. There are two methods for dequeueing views. The one you use depends on which type of view has been requested:

  • Use the dequeue​Reusable​Cell(with​Reuse​Identifier:​for:​) to get a cell for an item in the collection view.
  • Use the dequeue​Reusable​Supplementary​View(of​Kind:​with​Reuse​Identifier:​for:​) method to get a supplementary view requested by the layout object.

// 强调必须注册
Before you call either of these methods, you must tell the collection view how to create the corresponding view if one does not already exist. For this, you must register either a class or a nib file with the collection view. For example, when registering cells, you use the register(:​for​Cell​With​Reuse​Identifier:​) or register(:​for​Cell​With​Reuse​Identifier:​) method. As part of the registration process, you specify the reuse identifier that identifies the purpose of the view. This is the same string you use when dequeueing the view later.

0 0
原创粉丝点击