分阶段实施任务

时间:2015-01-23 21:28:27

标签: ios uitableview uicollectionview

我的每个单元格都有一个tableview,它是一个集合视图。

一开始我下载html并解析它。解析时,我希望加载集合视图。

但是集合视图开始搜索比html解析更早的单元格数。所以我有一个错误,因为那时我不知道我需要多少个细胞。

-(void)viewWillAppear:(BOOL)animated
{
    [self dataDownLoad];  
}

-(void)dataDownLoad{
    for (int i=0; i<urlArray.count; i++) {

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.test.ru/cat/%@",urlArray[i]]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                             timeoutInterval:60.0];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (response) {
                //--------------//-----------

                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[aNodes getAttributeNamed:@"href"], @"a", [imgNodes getAttributeNamed:@"src"], @"img", nil];

                [catArray addObject:dict];

                [contentArray addObject:catArray];

            }
        }];
    }

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [contentArray[collectionView.tag] count];
}

2 个答案:

答案 0 :(得分:0)

您应该返回0,直到加载数据为止。加载数据后,请将您的收藏视图告诉reloadData

答案 1 :(得分:0)

返回0并等待请求完成并处理您要处理的数据,然后致电reloadData() 您可能需要创建一个Outlet来引用集合视图,以便调用reloadData()

   -(void)viewWillAppear:(BOOL)animated
        {
        [self dataDownLoad];  
        }

    -(void)dataDownLoad{
        for (int i=0; i<urlArray.count; i++) {

            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.test.ru/cat/%@",urlArray[i]]];
            NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                 timeoutInterval:60.0];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                if (response) {
//--------------//-----------

                        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[aNodes getAttributeNamed:@"href"], @"a", [imgNodes getAttributeNamed:@"src"], @"img", nil];

                        [catArray addObject:dict];

                            [contentArray addObject:catArray];

                       //ADD THIS PART:
                        [collectionView reloadData];

                }
            }];
        }



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [contentArray[collectionView.tag] count];//this should return 0 the first time and be called again when reloadData() is called.
}