选择 UITableView 后如何推送加载视图?

发布于 2024-11-13 07:42:22 字数 205 浏览 5 评论 0原文

现在我在显示加载视图时遇到问题。这个过程是这样的:

  • 在 UITableView 中选择行
  • 从主视图弹出 UITableView
  • 将 Loadingview 推送到主视图
  • 调用使用 JSON 请求的长方法

问题是,当我选择表中的行时,它会在那里暂停,直到一切完成。我该如何解决这个问题?

Right now I am having trouble displaying the loading view. The process goes like this:

  • Select row in UITableView
  • Pop UITableView from mainview
  • Push Loadingview into mainview
  • Call long method that uses JSON Requests

The problem is that when I select the row in the table, it pauses there until everything is complete. How do I get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-11-20 07:42:22

您需要在单独的线程上加载数据。就我个人而言,我要做的是将加载视图推入主视图,然后生成一个加载数据的新线程。数据加载完成后,您可以编组回 UI 线程,关闭加载视图,然后呈现您将在其中显示数据的任何视图。

您的实现将如下所示:

// push loading view..
// spawn thread
[NSThread detachNewThreadSelector:@selector(loadData:) toTarget:self withObject:nil];

-(void)loadData:(id) obj {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // load your data
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:NO];
    [pool release];
}

-(void)updateUI:(id)data {
    // update the data
}

You'll want to load the data on a separate thread. Personally, what I'd do is push the loading view into the main view then spawn a new thread that loads the data. Once the data has loaded, you can marshall back to the UI thread, dismiss the loading view and then present whatever view it is that you'll be displaying your data in.

Your implementation will look something like this:

// push loading view..
// spawn thread
[NSThread detachNewThreadSelector:@selector(loadData:) toTarget:self withObject:nil];

-(void)loadData:(id) obj {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // load your data
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:NO];
    [pool release];
}

-(void)updateUI:(id)data {
    // update the data
}
我很坚强 2024-11-20 07:42:22

使用 GCD 并发送异步请求,当请求完成时转到主线程并执行您想要的操作。

//addProgressIndicator
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //call your request. Here could be your call back too, depends to architecture of your code
    NSDictionary *response = [WebService sendRequest];

    dispatch_async(dispatch_get_main_queue(), ^{

        //removeProgressIndicator
        //do what you want after request is finished with your UI

    });
});

Use GCD and send async request, when request finished go to main thread and do what you want.

//addProgressIndicator
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //call your request. Here could be your call back too, depends to architecture of your code
    NSDictionary *response = [WebService sendRequest];

    dispatch_async(dispatch_get_main_queue(), ^{

        //removeProgressIndicator
        //do what you want after request is finished with your UI

    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文