更新tableView而不阻塞主线程

时间:2012-07-17 14:16:20

标签: iphone objective-c ios ipad

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

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];
                }];
            });

        });

    }

现在上面的方法很重,因为我试图在仪器上对其进行分析。当滚动视图滚动时执行它然后它会很糟糕。所以我做的是等待滚动视图停止滚动并调用上面的方法,问题是它导致了非常糟糕的用户体验。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

UI更改只能在主线程上完成,也就是说,如果要更改文本,更改UIControl的帧等,必须在主线程中实现。

这是UIKit框架支持的内容