简体   繁体   English

动态加载UICollectionView的单元格

[英]Dynamically loading cells for UICollectionView

Hi I am very new for Ios and in my project I am using UICollectionView ok that's fine. 嗨,我对Ios非常陌生,在我的项目中,我使用的是UICollectionView,好的。

But here my main requirement is that I want to load the UICollectionView cells dynamically when we are scrolling the collectionView. 但是在这里,我的主要要求是我想在滚动collectionView时动态加载UICollectionView单元。

I mean when we launch the app then first "5" records need to load and when we are scrolling for the first time next "5" records need to load and when we scrolling for the second time the next "5" records need to be displayed and in that way all records need to load. 我的意思是,当我们启动该应用程序时,则需要加载第一个“ 5”记录;当我们第一次滚动时,需要加载下一个“ 5”记录;而当我们第二次滚动时,则需要加载下一个“ 5”记录显示,这样就需要加载所有记录。

Please help me. 请帮我。

How can I do this? 我怎样才能做到这一点?

my code:- 我的代码:

#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;
}

Solution 1: If you want to do infinite scrolling in a manner more like a desktop web app, you should implement the UIScrollViewDelegate protocol (or UICollectionViewDelegate , which is a superset) and listen for the scrollViewDidScroll : callback. 解决方案1:如果您想以类似于桌面Web应用程序的方式进行无限滚动,则应实现UIScrollViewDelegate协议(或UICollectionViewDelegate ,它是一个超集),并监听scrollViewDidScroll :回调。

Within that callback, iterate through the UICollectionView's indexPathsForVisibleItems and check to see if any of the index paths map to the 'last' item in your collection, which indicates the user has scrolled to the end. 在该回调中,循环访问UICollectionView的indexPathsForVisibleItems,并检查是否有任何索引路径映射到集合中的“最后一个”项目,这表明用户已滚动到末尾。

At that point, call your logic to load more stuff. 此时,请调用逻辑以加载更多内容。

Solution 2: 解决方案2:

In table view, you should load more data in willDisplayCell () method. 在表视图中,您应该在willDisplayCell ()方法中加载更多数据。 With collections, there is no such a counterpart. 对于集合,没有这样的对应物。 But, you can do like this. 但是,您可以这样做。 Hope, it helps. 希望能帮助到你。

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

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

May be it will help you. 可能会对您有帮助。

Solution: You can add infinite scrolling to your existing UICollectionView use the following open source library to archive your requirement. 解决方案:您可以使用以下开源库将无限滚动添加到现有的UICollectionView ,以存档您的需求。

UIScrollView-InfiniteScroll UIScrollView中,InfiniteScroll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM