iOS - 在AFNetworking之外使用responseObject

时间:2015-11-22 20:40:44

标签: ios objective-c uitableview afnetworking

我想在tableview单元格中显示从下面的AFNetworking调用中收到的responseObject。我在头文件(neighbourData)中创建了一个NSMutableArray属性,认为为它分配responseObject可能有效。虽然如预测的那样,我似乎无法使用AFNetworking方法之外的数据。另一方面,将AFNetworking方法放在我的tableview单元格中,但是等待数据在我的UIlabels中显示时存在滞后时间。有什么想法吗?

ViewController.h

@property (strong, nonatomic) NSMutableArray *neighbourData;

ViewController.m

 - (void)viewDidLoad {
        [super viewDidLoad];


        NSMutableDictionary *viewParams = [NSMutableDictionary new];
        [viewParams setValue:@"u000" forKey:@"view_name"];
        [DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

            self.neighbourData = [responseObject mutableCopy];
            NSLog(@"This is the neighbourdata %@",self.neighbourData);


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

       }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *NetworkTableIdentifier = @"sidebarCell";

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
    if (cell == nil)

    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

      NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
       [[cell username] setText:[userName objectForKey:@"users_name"]];

        NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
        [[cell userDescription] setText:[userBio objectForKey:@"userbio"]];


    return cell;

}

1 个答案:

答案 0 :(得分:0)

您可以使用responce对象初始化neighbourData,或者只是将其引用传递给您的arry,然后重新加载表。它会起作用

self.neighbourData = [NSMutableArray alloc]initWithArry: responseObject]];
[yourTableView reloadData];

OR

self.neighbourData = (NSMutableArray *)responseObject;
[yourTableView reloadData];