首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIRefreshControl滞后

UIRefreshControl滞后
EN

Stack Overflow用户
提问于 2014-12-28 12:01:12
回答 2查看 1.1K关注 0票数 0

我有一个包含用户帖子的表视图。每个帖子都有一张图片、用户名和帖子本身。刷新控件的操作是用Parse中的数据重新加载表。除了我拉动刷新时的极端延迟之外,一切都运行得很好。我不知道是因为每个单元格里的照片还是别的什么原因。如果有人知道为什么会这样,请让我知道。我会张贴的代码,如果有人需要它。

代码语言:javascript
复制
-(void)viewDidLoad {

    refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(retrieveFromParse)      forControlEvents:UIControlEventValueChanged];
refreshControl.backgroundColor = [UIColor whiteColor];
refreshControl.tintColor = [UIColor colorWithRed:0.3878 green:0.5686 blue:1.0 alpha:0.9];
[userPostsTable addSubview:refreshControl];

}


-(void) retrieveFromParse {

PFQuery *quoteQuery = [PFQuery queryWithClassName:@"Quotes"];
quoteQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
[quoteQuery orderByDescending:@"createdAt"];
[quoteQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

    if (!error) {

        NSString *category = [object objectForKey:@"Category"];

        if (category == nil) {

            quoteLabel.font = [UIFont fontWithName:@"Helvetica Neue Light Italic" size:15];

        }

        else {

            quoteLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15];

        }

        quoteString = [object objectForKey:@"quoteContent"];
        quoteLabel.text = quoteString;

        quoteIdString = object.objectId;
        [refreshControl endRefreshing];


    }

    else {

        [refreshControl endRefreshing];
    }
}];

PFQuery *userPostsQuery = [PFQuery queryWithClassName:@"userPosts"];
userPostsQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
[userPostsQuery setLimit:300];
[userPostsQuery whereKey:@"quoteObjectId" matchesKey:@"objectId" inQuery:quoteQuery];
if (segmentedControl.selectedSegmentIndex == 0) {

    [userPostsQuery orderByDescending:@"createdAt"];

}
else {

[userPostsQuery orderByDescending:@"likeCount"];

}
[userPostsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {

        userPostsArray = [[NSArray alloc] initWithArray:objects];
        [userPostsTable reloadData];
        [refreshControl endRefreshing];

    }

    else {

        [refreshControl endRefreshing];
    }
}];


}
EN

回答 2

Stack Overflow用户

发布于 2014-12-28 12:43:59

您正在后台线程中检索对象。

代码语言:javascript
复制
getFirstObjectInBackgroundWithBlock:

此外,您正在尝试从后台线程更新您的UI。你只需要从主线程更新UI:

调用所有与UI相关的调用,如:

代码语言:javascript
复制
[userPostsTable reloadData];
[refreshControl endRefreshing];

仅来自主线程。

使用:

代码语言:javascript
复制
dispatch_async(dispatch_get_main_queue(), ^{
  // Update UI
});
票数 1
EN

Stack Overflow用户

发布于 2014-12-28 12:43:50

假设您的代码片段是UITableViewController的一部分,则不应将refreshControl添加为tableViewsubview

代码语言:javascript
复制
-(void)viewDidLoad 
{
    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(retrieveFromParse)                  
                         forControlEvents:UIControlEventValueChanged];
    refreshControl.backgroundColor = [UIColor whiteColor];
    refreshControl.tintColor = [UIColor colorWithRed:0.3878 green:0.5686 blue:1.0 alpha:0.9];

    //This is causing the stutter
    //[userPostsTable addSubview:refreshControl];
    //UIRefreshControl is meant to be used by a UITableViewController
    self.refreshControl = refreshControl;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27673599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档