ios - 动态加载UICollectionView的单元格

标签 ios objective-c uicollectionview

嗨,我对Ios非常陌生,在我的项目中,我使用的是UICollectionView,好的。

但是这里我的主要要求是,当我们滚动collectionView时,我想动态加载UICollectionView单元。

我的意思是,当我们启动该应用程序时,则需要加载第一个“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;
}

最佳答案

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

UIScrollView-InfiniteScroll

关于ios - 动态加载UICollectionView的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487586/

相关文章:

swift - iOS 9 : Determine size of collection view in portrait and landscape

ios - 如何在 Swift 中以编程方式更改 UICollectionViewCell 大小?

ios - 在 swift 中,为什么我必须使用 IBaction 或 IBOutlet 在代码和 UI 之间进行通信?

iphone - UIButton在iOS7中没有显示突出显示

iphone - collectionView dequeueReusableCellWithIdentifier 出错

ios7 - 为什么 UITextView 在我开始输入时垂直居中

ios - 委托(delegate)似乎没有从 RBQFetchedResultController 触发

ios - 是否在任何地方记录了数组 `instantiateWithOwner` 返回以特定方式排序?

ios - appdelegate.m 中 View Controller 的 ivar

iOS 如何获取注解选择的索引?