简体   繁体   English

更新tableView而不阻塞主线程

[英]updating tableView without blocking main thread

So I have an iPad app with a UIScrollView. 所以我有一个带有UIScrollView的iPad应用程序。 The scroll view has around 5-10 UIView's shown at one time and inside those UIView's is a tableView. 滚动视图一次显示了大约5-10个UIView,而在这些UIView内部是tableView。 So essentially there are 5-10 UITableView's shown at one time. 因此,基本上一次显示了5-10个UITableView。 The issue is that when I scroll the UIScrollView it will call reloadData on the UITableView, in which in this case will set the text of the UITableView cell's. 问题是,当我滚动UIScrollView时,它将在UITableView上调用reloadData,在这种情况下,它将设置UITableView单元格的文本。 The method is as follows: 方法如下:

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

        });

    }

Now the method above is heavy, as I tried to profile it on instruments. 现在,上面的方法很繁琐,因为我尝试在仪器上进行分析。 When it is performed while the scroll view is scrolling then it lags so bad. 在滚动视图滚动时执行该操作时,它会滞后得如此糟糕。 So what I did was I waited until the scroll view stopped scrolling and call the method above, the issue is that it leads to a very bad UX. 所以我要做的是等到滚动视图停止滚动并调用上述方法后,问题是它导致了非常糟糕的UX。 Any ideas? 有任何想法吗?

The UI Changes only can be done on Main Thread, that is, If you want to change text, changing frame of a UIControl etc., musts be implemented in main thread. UI更改只能在主线程上完成,也就是说,如果要更改文本,更改UIControl的框架等,必须在主线程中实现。

that is what the UIKit framework supports 这就是UIKit框架所支持的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM