2012-07-17 7 views
0

그래서 UIScrollView가있는 iPad 앱이 있습니다. 스크롤보기 약 5-10 한 번에 표시되는 UIView 및 해당 UIView 안에 tableView입니다. 그래서 기본적으로 한 번에 5-10 개의 UITableView가 표시됩니다. 문제는 내가 UIScrollView를 스크롤하면이 경우 UITableView 셀의 텍스트를 설정하는 UITableView에 reloadData가 호출된다는 것입니다. 방법은 다음과 같습니다 :메인 스레드를 차단하지 않고 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]; 
       }]; 
      }); 

     }); 

    } 

위의 방법은 악기에 대한 프로파일 링을 시도 할 때 위의 방법이 무거웠습니다. 스크롤보기가 스크롤되는 동안 수행되면 너무 늦습니다. 스크롤 뷰가 스크롤을 멈추고 위의 메서드를 호출 할 때까지 기다렸습니다. 문제는 UX가 매우 좋지 않다는 것입니다. 어떤 아이디어?

+0

왜 reloadData를 호출해야합니까? –

+0

데이터 원본이 변경 되었기 때문에? – xonegirlz

+0

스크롤 할 때 다시로드했다고 생각 ... 데이터 소스가 얼마나 자주 변경됩니까? –

답변

0

UI 변경은 메인 스레드에서만 수행 할 수 있습니다. 즉, 텍스트를 변경하고 UIControl 등의 프레임을 변경하려면 메인 스레드에서 구현해야합니다.

UIKit 프레임 워크가 지원하는 항목

관련 문제