ios - AFNetWorking请求后存入NSArray中,numberOfRowsInSection:方法中返回数据个数为0
阿神
阿神 2017-04-18 09:40:37
0
4
689

先来点伪代码:

#import  //用来接收AFNetWorking请求的数据 @property (nonatomic, strong)NSArray *jsonData; @implementation xxxxViewController - (void)viewDidLoad { [super viewDidLoad]; [self getNetWorking]; } -(void)getNetWorking{ AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; [manager GET:@"http://127.0.0.1:8000/api/info/" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { //进度 //进度 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { self.jsonData=[xxxModel mj_ObjectArrayWithKeyValuesArray:responseObject[@"news"]]; [self.xxxx reloadData]; //此处能打印出获取到的数据信息 NSLog(@"%@",self.jsonData); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { // error 错误信息 }]; } @end

上面代码中请求后将responseObject返回的数据传给了self.jsonData,用NSLog打印后有值,但是为什么在UITableView的下面方法中获取到的数据个数为0,请问是什么原因,谢谢~!

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //打印数据个数 SSTLog(@"jsonData个数===%ld",self.jsonData.count); //这里为0 return self.jsonData.count; }

打印后的信息如下:


**折腾了一天终于搞定了原来是一个默认选中UITableView第一行的一个方法捣的鬼。重复测试了好几遍,再次感谢回复我的各位兄台,非常的感激~跪拜!
去掉下面的代码就好了。。。(不知道有没有替换这个的方法,有的话望能告知,非常感谢~!)**

//默认选中第一行 [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
阿神
阿神

闭关修行中......

Antworte allen (4)
大家讲道理

目前来看,代码没有问题。

出现这种现象的可能原因是,tableView 被添加到视图层级后,会调用多次numberOfRowsInSection,此时本地并没有数据,所以返回0。

当获取到数据后,才能显示正确的数量。

    Ty80

    获取到数据后,tableView reload一下

      Ty80
      @property (nonatomic, strong)NSArray *jsonData; 你定义的数组是不可变数组,不能进行再次赋值吧。使用NSMutableArray试试
        黄舟

        因为打印0的时候,那会网络数据还没有获取到..?

          Neueste Downloads
          Mehr>
          Web-Effekte
          Quellcode der Website
          Website-Materialien
          Frontend-Vorlage
          Über uns Haftungsausschluss Sitemap
          Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!