2

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

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

任何帮助表示赞赏,这真的很令人沮丧:(

4

3 回答 3

0

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

于 2013-07-04T08:53:57.753 回答
0

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

这样做时,json 数组将仅在setCompletionBlock.

由于您已经合成了 json 数组,因此您需要像这样分配它

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

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

于 2013-07-05T08:58:25.460 回答
0

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

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

或者,如果您不喜欢具有以下几行的界面构建器,则可以仅使用代码来完成

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

或者

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

要么/要么都应该工作。

于 2013-07-05T21:13:41.877 回答