【问题标题】:Failed to get data in cellForRowAtIndexPath by using model使用模型获取 cellForRowAtIndexPath 中的数据失败
【发布时间】:2018-11-15 01:17:18
【问题描述】:

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

MODEL.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

MODEL.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

}

【问题讨论】:

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


    【解决方案1】:

    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];
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多