2012-07-17 178 views
0

所以我有一个UIScrollView的iPad应用程序。滚动视图一次显示大约5-10个UIView,而在这些UIView中则是一个tableView。所以基本上一次显示5-10个UITableView。问题是,当我滚动UIScrollView时,它会调用UITableView上的reloadData,在这种情况下,它将设置UITableView单元格的文本。方法如下:更新tableView不会阻塞主线程

if (shouldUpdateComment){ 
     shouldUpdateComment = NO; 
     __block __weak AHCommentsTableViewCell * weakSelf = self; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 

      NSString *commentsText = [NSString stringWithFormat:@"%@ %@", self.imageComment_.username_, self.imageComment_.text_]; 
      NSMutableAttributedString* attrStr = [[[NSMutableAttributedString alloc] initWithString:commentsText] autorelease]; 

      NSRange usernameRange = [commentsText rangeOfString:self.imageComment_.username_]; 
      if (usernameRange.location != NSNotFound){ 
       [attrStr setTextColor:[UIColor colorWithRed:86.0/255.0 green:134.0/255.0 blue:172.0/255.0 alpha:1.0] range:usernameRange]; 

      } 

      NSString * url = [NSString stringWithFormat:@"userid://%@", self.imageComment_.id_]; 
      usernameRange = [commentsText rangeOfString:self.imageComment_.username_]; 
      if (usernameRange.location != NSNotFound){ 
       [weakSelf.commentsText_ addLink:[NSURL URLWithString:url] range:usernameRange]; 
      } 

      NSRange range; 
      range.location = 0; 
      range.length = commentsText.length; 

      [attrStr setFont:[UIFont fontWithName:@"HelveticaNeue" size:14] range:range]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       [weakSelf.commentsText_ setAlpha:0.0]; 
       [weakSelf.commentsPostedTime_ setAlpha:0.0]; 
       [weakSelf.commentsText_ setFrameWidth:self.contentView.frameWidth - self.profilePicture_.frameWidth - kCommentsPadding]; 
       [weakSelf.commentsText_ setFrameHeight:weakSelf.imageComment_.commentHeight_ - 30]; 
       [weakSelf.commentsText_ setAttributedString:attrStr]; 

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
        [weakSelf parseTagsInComment:commentsText]; 
       }); 

       NSString *timePosted = [NSString timestampToString:weakSelf.imageComment_.createdTime_]; 
       CGSize commentsTimeSize = [timePosted sizeWithFont:weakSelf.commentsPostedTime_.font constrainedToSize:CGSizeMake(weakSelf.commentsText_.frameWidth, 50)]; 
       [weakSelf.commentsPostedTime_ setText:timePosted]; 
       [weakSelf.commentsPostedTime_ setFrameWidth:commentsTimeSize.width]; 
       [weakSelf.commentsPostedTime_ setFrameHeight:commentsTimeSize.height]; 
       [weakSelf.commentsPostedTime_ setFrameY:weakSelf.commentsText_.frameY + weakSelf.commentsText_.frameHeight]; 
       [weakSelf.commentsPostedTime_ setFrameX:weakSelf.commentsText_.frameX]; 

       [UIView animateWithDuration:0.3 animations:^{ 
        [weakSelf.commentsText_ setAlpha:1.0]; 
        [weakSelf.commentsPostedTime_ setAlpha:1.0]; 
       }]; 
      }); 

     }); 

    } 

现在上面的方法很繁重,因为我试图在仪器上进行配置。当滚动视图滚动时执行它时,它滞后如此糟糕。所以我所做的是等到滚动视图停止滚动并调用上述方法时,问题在于它导致了非常糟糕的用户体验。有任何想法吗?

+0

为什么你必须调用reloadData? – 2012-07-17 14:41:14

+0

因为数据源更改? – xonegirlz 2012-07-17 15:47:17

+0

我以为您在滚动时重新加载......数据源多久更改一次? – 2012-07-17 15:59:53

回答

0

UI变化只能在主线程上完成,也就是说,如果你想改变文本,改变UIControl等的框架,必须在主线程中实现。

这就是UIKit框架支持的内容