延迟表视图在viewDidLoad之前加载或加载数组?

时间:2014-07-05 17:27:45

标签: ios objective-c arrays uitableview

我正在使用网络服务填充一系列项目,然后用于填充表格视图的单元格。

现在我遇到了表格视图本身的问题。当我检查“numberofrows”方法时,数组尚未加载。由于某种原因,它“正好”加载(根据我的nslogs和断点),即使我尽可能早地在每个加载方法中加载。

现在我不知道的是:

  • 有没有办法延迟创建表视图,所以我确定我的数组已加载?
  • 有没有办法加载我的数组?它目前是我在viewDidLoad中的第一行。 (调用另一个类,然后调用web服务并返回一个数组,但到那时,表视图已经加载并且为空)。

我尝试了什么:放一个tableview reloadData。但它根本行不通。由于某种原因,编译器会读取该行,但不会加载任何新行,即使此时阵列已满。

我错过了什么吗?

(如果我将硬编码对象放入我的数组中,我的tableview工作正常)

我可以编辑并添加一些你们要求的代码,但由于这看起来像学校问题,也许我只是忘记了什么。

伙计们,我都是耳朵! 编辑:

我的不同方法;我删除了不必要的代码以便更清楚地阅读。 编译器永远不会进入cellForRow,因为numberOfRows返回一个零数字。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CustomCellTableViewCell";
    CustomCellTableViewCell *cell = [self.tbUpcomingEvents dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];


    cell.lbTitle = [_eventList objectAtIndex:indexPath.row];

    return cell;
}

行数; nslog返回零。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numOfRows : %i", [_eventList count]);
    return [_eventList count];
}

我的webservice方法,这是从“webservice类”加载数据的方法。 在这里,NSLog显示一个完整的数组。

- (void)loadData:(NSMutableArray*)arrayEvent
{
//arrayEvent is full from the internet data. eventList is also full on the NSLog.
    _eventList = arrayEvent;
    NSLog(@"LoadData : %i", [_eventList count]);
    [self.tbUpcomingEvents reloadData];
}

我的viewdidLoad方法

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Calling my UseCaseController to load the data from the internet.
    UCC *sharedUCC = [UCC sharedUCC];
    [sharedUCC getUserEventsInDbDis:self :_user];

}

1 个答案:

答案 0 :(得分:0)

正如您所说,您正在使用网络服务。所以它在块中执行你的代码,这意味着它在单独的线程中运行。现在根据苹果文档UI更新应该发生在主线程中。因此,对于表创建,您需要在GCD代码中包含相同的内容: -

 dispatch_async(dispatch_get_main_queue,()^{

    // write your code for table creation here
  });
相关问题