如何重新加载UICollectionView的数据部分? [英] How to reload only data section of UICollectionView?

查看:935
本文介绍了如何重新加载UICollectionView的数据部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新加载UICollectionView的数据部分,而不是页眉或页脚(补充视图)。

I'm trying to reload only data section, not header or footer (supplementary view), of UICollectionView.

当我使用 reloadData时方法,页眉和页脚部分也重新加载,这就是我想要的。

When I use reloadData method, header and footer section also reloads, so that's what I want.

我找到了方法 reloadSections:,但我不知道为什么它不起作用。

I found the method reloadSections:, but I don't know why it doesn't work.

因为我的集合视图只包含一个部分,所以我尝试使用该方法像这样:

Because my collection view only contains one section, so I tried using the method like this:

[collectionViewController.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];

但调用方法时会出现运行时错误。错误的内容是:

But it makes runtime error when the method is called. The content of error is:

'NSInvalidArgumentException', reason: '-[UICollectionViewUpdateItem action]:
unrecognized selector sent to instance 0x90aec00'

方法是 reloadSections:不允许这样使用?有没有其他方法只重新加载数据部分,而不是页眉或页脚部分?

Is the method reloadSections: is not allowed to use like this? Is there any alternative way to reload only data section, not header or footer section?

推荐答案

我认为错误只发生在搜索栏是一个集合视图的子视图,并在键盘输入结束时尝试调用 reloadSections:

I think the error only occurs when the search bar is a collection view's subview, and trying to call reloadSections: when the keyboard input is up.

< a href =https://stackoverflow.com/questions/12894172/deleting-cells-from-uicollectionview-via-nsnotification>本文给出了类似问题的答案,但这不适用于我。

This article gives an answer to a similar problem, but that didn't work for me.

所以我使用了另一种方法来调用几乎相同的功能。

So I used an alternative way with which I can invoke almost the same feature.

而不是添加标题作为补充视图,我将标题作为视图的子视图。 (不是collectionView)

Instead of adding the header as a supplementary view, I made the header a subview of 'view'. (not collectionView)

并添加了一些滚动委托来模仿表格视图的标题。

And added some scroll delegate to mimic the table view's header.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < 44) {
        [self.searchBar setFrame:CGRectMake(0, -y, 320, 44)];
        [self.searchBar setHidden:NO];
    } else if (y >= 44) {
        [self.searchBar setHidden:YES];
    }  
} 

这篇关于如何重新加载UICollectionView的数据部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆