zoukankan      html  css  js  c++  java
  • iOS下UITableView的单元格重用逻辑

    终于有时间继续UITableView的接口调用顺序这篇文章了,之前测试过,模拟器都是按照height,cellForRow这样的顺序调用接口的,iOS8以前一直是这样,但是到了iOS8,这个顺序倒过来了这样倒是有好处,至少按照传统思路,单元格先创建,创建完成后就知道高度了.但这样怎么向下兼容呢,来试试这个粗略的办法.
    在cellForRow,heightForRow两个接口中都调用一个函数,来创建单元格,但是要根据系统版本做判断区分

     1 -(H5TableCell*)tableView:(UITableView *)tableView makeCellForRowAtIndexPath:(NSIndexPath *)indexPath mode:(BOOL)isOnCellCreate
     2 {
     3     NSString* cellItemStyleNameStr = [self getCellStyle:indexPath];
     4     UIView* contentView = nil;
     5     H5TableCell  *cell = nil;
     6     if ((isOnCellCreate && ISIOS8) || (!isOnCellCreate && ISIOS7 && !ISIOS8))
     7     {
     8         cell =[tableView dequeueReusableCellWithIdentifier:cellItemStyleNameStr];
     9         if (cell==nil)
    10         {
    11             cell=[[H5TableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellItemStyleNameStr];
    12             contentView = [self tableView:tableView viewForRowAtIndexPath:indexPath];
    13             [cell.contentView addSubview:contentView];
    14             cell.contentView.frame = CGRectMake(0, 0, contentView.frame.size.width, contentView.frame.size.height);
    15         }
    16         else
    17         {
    18             NSUInteger idx = [cellArray indexOfObject:cell];
    19             if (NSNotFound != idx)
    20             {
    21                 NSIndexPath* reusedIndexPath = cell.IndexPath;
    22                 //KCLog(@"复用%ld.%ld for %ld.%ld",reusedIndexPath.section,reusedIndexPath.row,indexPath.section,indexPath.row);
    23                 
    24                 //清除H5Core库登记的对该被复用indexPath单元的view登记
    25                 NSString* windowKey = [NSString stringWithFormat:@"%@_%ld_%ld",
    26                                        cellItemStyleNameStr,
    27                                        (long)reusedIndexPath.section,
    28                                        (long)reusedIndexPath.row];
    29                 //[[H5Core shareInstance] destroyH5CoreWindow:windowKey];
    30                 [self.delegate destructionWindowName:windowKey];
    31                 [cellArray removeObject:cell];
    32                 [cellIdArray removeObjectAtIndex:idx];
    33             }
    34             else
    35             {
    36                 KCLog(@"复用 未找到 for %ld.%ld",indexPath.section,indexPath.row);
    37             }
    38         }
    39         cell.IndexPath = indexPath;
    40         cell.delegate = self;
    41         cell.selectionStyle=UITableViewCellSelectionStyleNone;
    42         cell.backgroundColor=[UIColor clearColor];
    43         [cellArray addObject:cell];
    44         [cellIdArray addObject:indexPath];
    45     }
    46     else
    47     {
    48         //只是获取cell,因为已经创建过了
    49         NSUInteger idx = [cellIdArray indexOfObject:indexPath];
    50         if (NSNotFound != idx)
    51         {
    52             cell = [cellArray objectAtIndex:idx];
    53         }
    54         else
    55         {
    56             KCLog(@"非复用 未找到 for %ld.%ld",indexPath.section,indexPath.row);
    57         }
    58         
    59     }
    60 
    61     return cell;
    62 }

    目前没有发现有复用未找到的情况.

  • 相关阅读:
    多角度分析平台即服务?PaaS的类型和用例
    2021.3.10 Android导出Excel表
    2021.3.9 个人作业阶段2
    2021.3.8 Android图像视图1
    2021.3.7 Android图像视图
    2021.3.6 Android页面刷新
    2021.3.5 个人作业1+家庭记账本(8)
    2021.3.4 家庭记账本(7)
    2021.3.3 Android项目打包+家庭记账本(6)
    2021.3.2 开课博客+家庭记账本(5)
  • 原文地址:https://www.cnblogs.com/decwang/p/4755420.html
Copyright © 2011-2022 走看看