【问题标题】:Adding UIActivityIndicatorView on Synchronous Web Service data and Populating UITableView在同步 Web 服务数据上添加 UIActivityIndi​​catorView 并填充 UITableView
【发布时间】:2013-11-02 09:23:09
【问题描述】:

我正在通过同步方法从 Web 服务中获取数据。我向 Web 服务发出请求,然后视图冻结。我尝试在从 Web 服务加载数据之前添加 UIActivityIndi​​catorView,并在获取数据后停止它但不显示活动指示器。 我试图将 Web 服务数据获取操作放在不同的线程上

[NSThread detachNewThreadSelector:@selector(fetchRequest) toTarget:self withObject:nil];

但此时 TableView 崩溃,因为它没有获取用于绘制单元格的数据。 在我正在做的 fetchRequest 函数中

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                                                          URLWithString:URLString]];
        NSData *response = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:nil error:nil];

        NSError *jsonParsingError = nil;
        NSDictionary *tableData = [NSJSONSerialization JSONObjectWithData:response
                                                              options:0
                                                                error:&jsonParsingError];


        responseArray = [[NSMutableArray alloc]initWithArray:[tableData objectForKey:@"data"]];

        for(int i = 0; i < responseArray.count; i++)
        {
            NSArray * tempArray = responseArray[i];
            responseArray[i] = [tempArray mutableCopy];
        }

这个responseArray用于填写单元格中的信息 请告诉我如何做到这一点。任何帮助将不胜感激...

【问题讨论】:

    标签: ios iphone objective-c uitableview uiactivityindicatorview


    【解决方案1】:

    问题在于你的方法。 Synchronous 方法在主线程上运行。由于主线程上的 UI 更新,您的应用程序挂起

    因此,解决方案是使用 asynchronous 方法在单独的线程上下载数据,这样您的 UI 就不会挂起

    所以,请使用NSURLConnectionsendAsynchronousRequest。这是一些示例代码:

    NSURL *url = [NSURL URLWithString:@"YOUR_URL_HERE"];
    
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        //this is called once the download or whatever completes. So you can choose to populate the TableView or stopping the IndicatorView from a method call to an asynchronous method to do so.
    }]; 
    

    【讨论】:

    • 如何用这个填充 uitableview 数据,因为它在创建 tableview 单元格时崩溃。
    【解决方案2】:

    您最好使用 Grand Central Dispatch 来获取这样的数据,以便在后台队列中调度它,并且不要阻塞也用于 UI 更新的主线程:

     dispatch_queue_t myqueue = dispatch_queue_create("myqueue", NULL);
        dispatch_async(myqueue, ^(void) {
    
        [self fetchRequest];
     dispatch_async(dispatch_get_main_queue(), ^{
                // Update UI on main queue
     [self.tableView reloadData];
            });
    
        });
    

    关于您可以在解析开始时使用的活动指示器:

    [self.activityIndicator startAnimating];
    self.activityIndicator.hidesWhenStopped = YES
    

    然后当你的表格填满数据时:

    [self.activityIndicator stopAnimating];
    

    【讨论】:

    • 但问题是它会崩溃,因为 tableview 单元格同时加载到主线程上。
    • @Atul 编辑了我的答案。试试这个。如果这不起作用,则崩溃可能与您填充表格单元格的方式有关。
    • 如何填充表格单元格?
    • 你实现了 UITableViewDelegate 方法了吗?像 cellForRowAtIndexPath?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多