【问题标题】:iOS - Use responseObject outside of AFNetworkingiOS - 在 AFNetworking 之外使用 responseObject
【发布时间】:2015-11-22 20:40:44
【问题描述】:

我想在我的表格视图单元格中显示从下面的 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;

}

【问题讨论】:

  • 为什么你没有在收到回复后立即重新加载表格..
  • self.neighbourData = [responseObject mutableCopy]; 在此行之后不久,您应该重新加载表。还要确保你的表数据源是这个邻居数据。也尝试以不同的方式对 nsarray 进行种姓,例如 self.neighbourData = (NSMutableArray *)responseObject;

标签: ios objective-c uitableview afnetworking


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    相关资源
    最近更新 更多