动态加载UICollectionView的单元格

时间:2015-12-28 04:13:25

标签: ios objective-c uicollectionview

嗨,我是Ios的新手,在我的项目中,我使用的是UICollectionView,这很好。

但是我的主要要求是我想在滚动collectionView时动态加载UICollectionView单元格。

我的意思是当我们首先启动应用程序时#3; 5"记录需要加载,当我们第一次滚动时," 5"记录需要加载,当我们第二次滚动时,下一个" 5"需要显示记录,并以这种方式加载所有记录。

请帮帮我。

我该怎么做?

我的代码: -

#import "ViewController.h"

@interface ViewController ()
{
    UICollectionView * _collectionView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) collectionViewLayout:layout];

    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:_collectionView];


    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, _collectionView.frame.size.height - 50, 0, 0)];
    [_collectionView addSubview:refreshView];

    UIRefreshControl *refreshControl = [UIRefreshControl new];
     refreshControl.tintColor = [UIColor redColor];
   [refreshControl addTarget:self action:@selector(viewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
    [refreshView addSubview:refreshControl];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    for (id subview in cell.contentView.subviews) {
        if ([subview isKindOfClass:[UIImageView class]]) {
            [subview removeFromSuperview];
        } else if ([subview isKindOfClass:[UILabel class]]) {
            [subview removeFromSuperview];
        }
    }

    cell.backgroundColor = [UIColor lightGrayColor];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(70, 70);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    UIEdgeInsets insets=UIEdgeInsetsMake(10, 10, 10, 10);
    return insets;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return 30.0;
}

2 个答案:

答案 0 :(得分:0)

解决方案1:如果您希望以更像桌面网络应用的方式进行无限滚动,则应实施UIScrollViewDelegate协议(或UICollectionViewDelegate,即一个超集)并听取scrollViewDidScroll:回调。

在该回调中,遍历UICollectionView indexPathsForVisibleItems并检查是否有任何索引路径映射到最后一个'集合中的项目,表示用户已滚动到最后。

此时,请调用您的逻辑来加载更多内容。

解决方案2:

在表格视图中,您应该在willDisplayCell()方法中加载更多数据。对于收藏品,没有这样的对应物。但是,你可以这样做。希望,它有所帮助。

(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath 
{
// Other code here ...

    if (indexPath.item == [self.data count] - 1) {
        [self loadMoreData];
    }
}

可能会帮助你。

答案 1 :(得分:0)

解决方案:您可以向现有UICollectionView添加无限滚动,请使用以下开源库来存档您的需求。

UIScrollView-InfiniteScroll

相关问题