我有一个 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/