终于有时间继续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 }
目前没有发现有复用未找到的情况.
转载请注明出处.
写博客的目的:记录,升华,量变到质变