【问题标题】:ios - ASIHTTPREQUEST populating uitableview with json dataios - ASIHTTPREQUEST 用 json 数据填充 uitableview
【发布时间】:2013-07-04 06:34:57
【问题描述】:

我已经阅读了几个小时,但我无法弄清楚为什么我的表格不会用更新的数据重新加载自己,这是我以块形式提出的请求:

 ASIHTTPRequest *_request= [ASIHTTPRequest requestWithURL:url];
__weak ASIHTTPRequest *request = _request;
request.requestMethod = @"POST";
[request addRequestHeader:@"Content-Type" value:@"application/json"];


[request setDelegate:self];
[request setCompletionBlock:^{
    NSString *responseString = [request responseString];
    NSData *responseData = [request responseData];
    NSLog(@"Response: %@", responseString);
    NSError* error;
    json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"request finished");
    [CommMTable reloadData];


}];
[request setFailedBlock:^{
    NSError *error = [request error];
    NSLog(@"Error: %@", error.localizedDescription);
}];

[request startAsynchronous];

它成功获取了 json 对象,因为它在我的控制台中打印,但表格没有更新!

这是我的表编译指示部分

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.json count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier=@"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSDictionary *tempDictionary= [self.json objectAtIndex:indexPath.row];



    if(cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    cell.textLabel.text = [tempDictionary objectForKey:@"title"];


    return cell;
}

感谢任何帮助,这真的很令人沮丧:(

【问题讨论】:

  • 您应该使用NSNotificationCentre 并重新加载您的表格。在此过程中,通知将通知您的自定义方法,现在它有数据并使用重新加载表,然后您的表视图中将有数据
  • 我一定会尝试一下,我已经看到了一些“应该”做我想做的事的例子。

标签: ios uitableview asihttprequest


【解决方案1】:

你检查过json数组是否更新了吗?并且您在编译指示部分中使用了 json 数组作为属性(self.json),而不是在内部块中。

【讨论】:

  • json 是我的 .h 文件中声明的数组,我在 .m 文件中合成了它。
【解决方案2】:

json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

执行此操作时,json 数组将仅在 setCompletionBlock 范围内可用。

既然已经合成了json数组,就需要像这样赋值

self.json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

如果这也不起作用,只需检查 json 的格式是否正确,并且您正在正确解析 cellForRowAtIndexPath 中的 json。

【讨论】:

    【解决方案3】:

    我发现了问题,我做的一切都是正确的(json 格式和东西设置正确)。问题是由于某种原因,该表与它的委托和数据源断开连接,遇到类似问题的人可以尝试此解决方案。

    在 xib 文件中,只需 ctrl + 将 tableview 拖到 File Owner 并将其连接到委托和数据源。

    如果你不喜欢下面这几行的界面生成器,你也可以只用代码来完成

    [myTableView setDataSource:self];
    [myTableView setDelegate:self];
    

    self.tableView.delegate = self.tableDelegate;
    self.tableView.datasource = self.tableDelegate; 
    

    要么/要么都应该工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      相关资源
      最近更新 更多