2012-07-17 134 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框架支持的內容