如何自动重新加载集合视图?

时间:2013-09-23 23:51:34

标签: ios uicollectionview reloaddata

我使用集合视图开发了一个RSS阅读器应用程序。我的问题是,在第一次发布时,应用程序出现空白。

过程是,RSS源从Web中提取,存储到设备上的临时文件中,然后通过集合视图显示。

我的问题是如何在文件加载后让应用程序自动重新加载数据?这样用户就不会看到初始的空白屏幕。

我尝试添加此代码

       [self.collection1 performBatchUpdates:^{
       [self.collection1 reloadSections:[NSIndexSet indexSetWithIndex:0]];
   } completion:nil];

但是它对初始视图不起作用。

我应该从appdelegate做些什么吗?或者从集合视图控制器本身?

请告知

以下是用于获取和显示我的数据的代码....

#pragma mark - UICollectionViewDataSourceDelegate
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  {
  return 1;
  }

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

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"articleCellmain"
                                                                           forIndexPath:indexPath];
NSDictionary *item = [_articleListmain objectAtIndex:indexPath.item];

// set the article image
[cell.image setImageWithURL:[item objectForKey:@"image"]];

// set the text of title UILabel
cell.title.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"title"]];
cell.title.textColor = [UIColor colorWithRed:33.0f/255.0f green:74.0f/255.0f blue:146.0f/255.0f alpha:1.0f];

// set the text of summary UILabel
cell.description.text = [NSString stringWithFormat:@"%@\n\n\n\n\n\n\n\n\n", [item objectForKey:@"description"]];


cell.targetURL.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"link"]];

cell.category.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"category"]];

cell.date.text = [NSString stringWithFormat:@"%@\n\n\n\n", [item objectForKey:@"pubDate"]];

cell.image.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"placement.png"]];


return cell;


}

4 个答案:

答案 0 :(得分:9)

一旦知道下载已完成,您应该可以使用[collectionView reloadData]。

答案 1 :(得分:4)

试试这个

 [self.collection1 performBatchUpdates:^{
   [collectionView reloadData]
 } completion:nil];

下载完成后。

答案 2 :(得分:1)

[self.collection1 performBatchUpdates:^{
[self.collection1 reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];

尝试使用以下代码替换此代码

[self.collection1 performBatchUpdates:^{
[self.collection1 reloadData];
} completion:nil];

答案 3 :(得分:1)

我认为您只需将[self.collection1 reloadData]放在ViewController的viewWillAppear中,其中放置了您的集合视图。

OR

您甚至可以在视图出现之前将其重新加载到某处,就像解析后一样,只需确保在重新加载之前初始化了集合视图所在的视图。