iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法

2020-01-21 05:06:03王振洲

1.发现问题

今天一早来公司,一个同事举着他的6p对我们说:“你看看这是嘛啊...怎么划不动啊...”我一看,果然,滑两下TableView,大概加载2页多就卡飞了...顿时想以是他机子太老了,物理内存不够用balabala等等原因回怼时...人家后面又说了一句:“你看人家今日头条怎么滑都没事~”。

好吧,我看看好吧。

ios,tableview卡顿,tableview滑动卡顿,ios开发tableview卡顿
虽然是在iPhone X上录的,但上下滑动卡顿依旧非常明显

2.排除问题

没错,我和你想的一样,十有八九应该是那几个老问题导致的:

Cell高度计算问题:把同事写的SD自动计算行高写死后问题依旧存在。先排除!


///行高写死后依旧卡顿
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 //return [self.tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[JstyleNewsOnePlusImageVideoViewCell class] contentViewWidth:kScreenWidth];
return 200;
}

Cell上子控件大小位置异步渲染问题:把Cell上所有同事写的SDLayout约束全部注释掉后,问题依旧存在。先排除!(代码略了)

Cell没有被TableView注册复用:检查并更换DataSource方法后,确认注册、复用cell没有问题。先排除!


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 //省略部分防崩溃判断代码...
 JstyleNewsHomePageModel *model = self.dataArray[indexPath.row];
 switch ([model.type integerValue]) {
 case 1:{
  if ([model.head_type integerValue] == 1 && [model.isImageArticle integerValue] == 1) {
  static NSString *ID = @"JstyleNewsOnePlusImageArticleViewCell";
  /*换一种Cell复用方法,效果依旧卡顿,证明TableViewCell复用没有问题。
  JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  if (!cell) {
   cell = [[JstyleNewsOnePlusImageArticleViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  }
  */
JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
  ///剧透:卡顿的原因就在这!Cell重复注册3DTouch预览!后面会说解决办法。
  [self registerForPreviewingWithDelegate:self sourceView:cell];
  if (indexPath.row < self.dataArray.count) {
   cell.model = model;
  }
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  return cell;
  } //else if...
 //case 2:...
}

内存泄露:使用instrument监控并测试后,除了几个第三方SDK导致的Leek之外,并没有自己调用方法产生的泄露。(本宝宝对instrument的使用还比较肤浅,后面会再仔细琢磨,大哥们勿喷...)先排除!