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

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

ios,tableview卡顿,tableview滑动卡顿,ios开发tableview卡顿
UShareSDK和XMPPFramework中有泄露

3.定位问题

既然导致TableView卡顿的几大原因都排除了,那就要考虑额外的因素了。折腾这一顿后,静下心来仔细回忆最近到底有没有改过首页的TableView。然后...好像...好像...前些天闲来无事我是在首页加了一个3DTouch重按预览的功能!难道...
怀着鸡冻的心情仔细检查了一遍3DTouch转场代理方法,发现并木有什么问题:


#pragma mark - 3DTouch预览
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
 NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
 if ([self.dataArray[indexPath.row] isImageArticle].integerValue == 1) {
  JstylePictureTextViewController *pictureVC = [[JstylePictureTextViewController alloc] init];
  if (indexPath.row < self.dataArray.count) {
   pictureVC.rid = [self.dataArray[indexPath.row] id];
   CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,[self.tableView cellForRowAtIndexPath:indexPath].height);
   previewingContext.sourceRect = rect;
  }
  return pictureVC;
  
 } else {
  JstyleNewsArticleDetailViewController *detailVC = [[JstyleNewsArticleDetailViewController alloc] init];
  detailVC.preferredContentSize = CGSizeMake(0.0f,500.0f);
  if (indexPath.row < self.dataArray.count) {
   detailVC.rid = [self.dataArray[indexPath.row] id];
   detailVC.titleModel = self.detailDataArray[indexPath.row];
   CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,[self.tableView cellForRowAtIndexPath:indexPath].height);
   previewingContext.sourceRect = rect;
  }
  return detailVC;
 }
}

然后就又迷茫了,到底问题在哪?上个厕所,嘘嘘一下,冷静冷静...果然,卫生间是一个伟大的地方...提裤子的时候突然想到一个重大问题!3DTouch预览是需要提前注册代理并告知控制器SourceView是谁的!而这个注册方法...好像有点子问题:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *ID = @"JstyleNewsOnePlusImageArticleViewCell";
  JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  if (!cell) {
   cell = [[JstyleNewsOnePlusImageArticleViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  }
  //!!!是他是他就是他!!!每一次滑动TableView复用Cell的时候都会注册一遍3DTouch代理!不卡才怪了!
  //[self registerForPreviewingWithDelegate:self sourceView:cell];注释掉之后,瞬间“纵享丝滑”!

  if (indexPath.row < self.dataArray.count) {
   cell.model = model;
  }
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  return cell;
}