ResponseObject到UITableView

时间:2015-12-13 21:25:31

标签: ios objective-c uitableview afnetworking

我已经使用AFNetworking发出了POST请求,然后我发出了GET请求:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Info: %@", responseObject);dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

    self.tableViewObject.dataSource = self;
    self.tableViewObject.delegate = self;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这里我得到一个responseObject。我想这样做,当你点击UITableView中的responseObject时,我进入一个包含responseObject中所有参数的新视图。我可以记录responseObject但是我无法将它放入我的UITableView中。我的TableView看起来像这样:

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}

在.h:

@interface MyCardsVC : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *dataArray;
}

@property (weak, nonatomic) IBOutlet UITableView *tableViewObject;

@end

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要在ViewDidLoad中设置数据源和委托

self.tableViewObject.dataSource = self;
self.tableViewObject.delegate = self;

另外,请确保在获得responseObject调用时[self.tableViewObject reloadData]。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Info: %@", responseObject);dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

    [self.tableViewObject reloadData];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];