美文网首页
关于UITableViewCell重用机制的理解

关于UITableViewCell重用机制的理解

作者: 木子尚武 | 来源:发表于2016-09-28 15:06 被阅读365次
  • UITableViewCell的重用机制体现在-(UITableViewCell)dequeueReusableCellWithIdentifier:(NSString)identifier这个方法中,他的基本意思就是在创建cell的时候为每一个cell都绑定一个identifier的标识。当cell从我们的视觉范围中消失的时候,这个绑定了cell的标识就会被放到缓存池中。当tableView需要新的cell的时候,直接先去缓存池中寻找有没有携带identifier的cell,若有的话直接复用;没有的话,才去创建新的cell,并绑定标识identifier。所以从理论上讲,倘若一屏最多显示的cell个数为n个,那么需要携带identifier表示的cell最少只需n+1个。
  • 当然在某些情况下,我们不需要复用cell,因为cell的复用在当cell的个数比较少的情况下,对于性能的提升几乎没有作用,反而有可能会造成数据的错乱,这时候我们不想再使用cell的重用机制,这时候该怎么办呢?可以利用以下几种方式来解决:
       方法1 将获得cell的方法从- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    

重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。

示例代码:

[plain]

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //...其他代码
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //...其他代码
    }

方法2 通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决。
重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。于是我们将每个cell的标识符都设置为不同,就可以避免不同cell重用的问题了。

示例代码:

[plain]

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //...其他代码
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //...其他代码
    }

方法3 删除重用cell的所有子视图

这个方法是通过删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。

示例代码:

[plain]

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    else
    {
    //删除cell的所有子视图
    while ([cell.contentView.subviews lastObject] != nil)
    {
    [(UIView
    )[cell.contentView.subviews lastObject] removeFromSuperview];
    }
    }
    //...其他代码
    }

-  js中的逻辑与(&&)和逻辑或(||):  
  在js中写出如下的答案 :
  var a = 2;
  var b = 3;
  var andflag = a && b ;
  var orflag = a || b;
  乍一看,这是一道逻辑或和逻辑与的运算题,然而在webostorm中运行一下才发现结果是,andflag  = 3,orflag = 2;
  具体的执行原理如下:

在运算过程中,首先js 会将 && 和|| 两边的值转成Boolean 类型,然后再算值 ,&&运算如果返回true,  
则取后面的值,如果|| 返回true,则取前面的值 , 而其中数值转换成boolean 的规则 是:
对象、非零整数、非空字符串返回true,
其它为false ;

相关文章

网友评论

      本文标题:关于UITableViewCell重用机制的理解

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