Rove~ 2017-02-02 03:21 采纳率: 0%
浏览 2861

iOS tableView在cellForRowAtIndexPath中读取数组为空的问题

在viewDidload中实现异步读取数据后,在数组(NSMutableArray)中添加此对象数据,刷新tableView
结果在numberOfRowsInSection可以获取到该数组中的对象,但在cellForRowAtIndexPath中获取到得数组却为空,求大神帮忙看看

- (NSMutableArray *)noticeArray
{
    if (!_noticeArray) {
        _noticeArray = [NSMutableArray array];
    }
    return _noticeArray;
}

下面是同过异步请求得到的数据,并添加进数组,刷新tableView

  NSMutableArray *noticesArray = [NSMutableArray array];
        for (NoticeWithNews *notice in array) {
            NoticeResult *model = [[NoticeResult alloc] init];
            model.noticeWithNews = notice;
            [noticesArray addObject:model];
        }       
         self.noticeArray = noticesArray;        
        [self.tableView reloadData];

至此都还没出现问题,打印的 self.noticeArray也有添加的对象
下面是numberOfRowsInSection,刷新tableView之后打印的self.noticeArray.count有值。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"%ld",self.noticeArray.count);
    return self.noticeArray.count;
}

但是到cellForRowAtIndexPath中打印的self.noticeArray,就一直为空

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *identifier = @"identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    NSLog(@"%@",self.noticeArray[indexPath.row]);


    return cell;
}

2017-02-02 11:15:31.164 校园新闻[2555:68321] 4
2017-02-02 11:15:31.164 校园新闻[2555:68321] (null)
2017-02-02 11:15:31.165 校园新闻[2555:68321] (null)
2017-02-02 11:15:31.166 校园新闻[2555:68321] (null)
2017-02-02 11:15:31.167 校园新闻[2555:68321] (null)


求大神帮忙解答下,为什么在cellForRowAtIndexPath拿到的数组self.noticeArray会突然变为NULL

  • 写回答

2条回答

  • 普通网友 2017-02-02 04:04
    关注

    这里的数组建议你全部使用_array这样调用,不要懒加载。数组你懒加载有什么用。

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突