ios - 使用模型无法在 cellForRowAtIndexPath 中获取数据

标签 ios objective-c json model-view-controller

我有一个 json API 文件,我想填充到我的 tableview 单元格中。 但是,我没有得到数据,如果你们能找出我的错误在哪里,我将不胜感激。谢谢!

ViewController.m cellForRowAtIndexPath 中的值将变为空。如何将我的 json 数据传递给 AddressItem 模型和

@property (strong , nonatomic)NSMutableArray<AddressItem *> *aryAddressData;
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@property (nonatomic,strong) NSMutableArray *aryAddressList;

- (void)setUpAdrressData
{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    ...
    manager.requestSerializer = serializer;


    NSDictionary *params = @{@"user_id": @(1)};

    [manager POST:@"http://.net/api/member_address" parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {

        _aryAddressList = responseObject;
        self.aryAddressData=[NSMutableArray array];
        for (NSDictionary *subDic in _aryAddressList) {

            AddressItem *model=[[AddressItem alloc]initWithDic:subDic];
            [self.aryAddressData addObject:model];
            NSLog(@"110 %@",model.address_1);
            NSLog(@"110 %@",model.address_2);
           //I CAN GET VALUE HERE
        }

    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"IF ERROR");
    }];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:AddressCellID forIndexPath:indexPath];
    cell.addressItem = _aryAddressData[indexPath.row];
    return cell;
}

模型.h

#import "JSONModel.h"
#import <Foundation/Foundation.h>

@interface AddressItem : JSONModel

@property (nonatomic, copy) NSString *address_1;
@property (nonatomic, copy) NSString *address_2;

- (instancetype)initWithDic:(NSDictionary *)dic;

@end

模型.m

#import "AddressItem.h"
#import "NSString+Utils.h"

@implementation AddressItem

- (instancetype)initWithDic:(NSDictionary *)dic{
    NSError *error = nil;
    self =  [self initWithDictionary:dic error:&error];
    return self;
}

VIEWCELL.h

#import <UIKit/UIKit.h>
@class AddressItem;
@interface AddressCell : UITableViewCell
@property (strong , nonatomic)AddressItem *addressItem;
@end

VIEWCELL.m

- (void)setAddressItem:(AddressItem *)addressItem
{
    _addressItem = addressItem;
    NSLog(@"202 - %@", addressItem.address_1);
    NSLog(@"202 - %@", addressItem.address_2);
    //NO VALUE HERE AS WELL

}

最佳答案

调用Api是异步的,填完数组需要重新加载表格

for (NSDictionary *subDic in _aryAddressList) {

    AddressItem *model=[[AddressItem alloc]initWithDic:subDic];
    [self.aryAddressData addObject:model];
    NSLog(@"110 %@",model.address_1);
    NSLog(@"110 %@",model.address_2);

}
dispatch_async(dispatch_get_main_queue(), ^{
  [self.tableView reloadData];
});

关于ios - 使用模型无法在 cellForRowAtIndexPath 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50702006/

相关文章:

javascript - 如何使用一维数组形成 JSON 数组?

java - 启用默认输入后,是否有办法覆盖 Jackson 输出的类型?

ios - 如果应用程序正在运行并且配置文件显示 "Invalid Status"会发生什么?

iOS 可以在 UITabbar 上使用可拖动的隐藏按钮吗?

objective-c - 如何从 parse.com 检索图像

objective-c - 列名的 NSPredicate 问题

iphone - 如何绘制特定颜色的CGGradient?

ios - UITableView -headerViewForSection 返回(空)

objective-c - 如何检测某些字符是否位于 NSString 的末尾?

Java:解析 JSON 文件