数据重新加载-UICollectionView-目标C

时间:2018-07-12 15:28:31

标签: objective-c uicollectionview reloaddata

我不知道会发生什么,但是在集合视图上调用reloadData不会导致任何结果。什么都没发生。也许我缺少了一些东西,我只是不知道什么。

我已经尝试了启动时的数据,并且可以正常工作,并且填充了收集视图。但是,获取远程数据然后再调用reloadData不会填充集合视图。

提前谢谢

班级在下面:

FoodChannelsCollection.h

#import <UIKit/UIKit.h>
#import "YouTubeDataAPI.h"

@interface FoodChannelsCollection: UIViewController <YouTubeDataAPIDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@end

FoodChannelsCollection.m

#import "FoodChannelsCollection.h"
#import "ChannelCell.h"
#import "UIImageView+ImageLoader.h"

@interface FoodChannelsCollection()

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation FoodChannelsCollection

//@synthesize collectionView;

NSMutableArray *channels;

//-------------------------------------------------------------------------//
// MARK: Controller's main view setup
//-------------------------------------------------------------------------//

- (void)viewDidLoad
{
    [super viewDidLoad];

    channels = [NSMutableArray new];

    YouTubeDataAPI *dataApi = [YouTubeDataAPI getInstance];
    dataApi.delegate = self;
    [dataApi fetchFirstChannelsBatch];
}

//-------------------------------------------------------------------------//
// MARK: YouTubeDataAPIDelegate
//-------------------------------------------------------------------------//

- (void)getFirstChannelsBatch:(NSArray *)firstChannelsBatch
{
    if (firstChannelsBatch == nil) { return; }

    NSLog(@"firstChannelsBatch count: %i", [firstChannelsBatch count]);

    if ([firstChannelsBatch count] == 0) { return; }

    NSLog(@"firstChannelsBatch count(2): %i", [firstChannelsBatch count]);

    [channels addObjectsFromArray: firstChannelsBatch];

    NSLog(@"Channels count (Del): %i", [channels count]);

    [self.collectionView reloadData];
}

//-------------------------------------------------------------------------//
// MARK: UICollectionViewDelegateFlowLayout
//-------------------------------------------------------------------------//

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    int width = [UIScreen mainScreen].bounds.size.width;

    return CGSizeMake(width/2, width/2);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//-------------------------------------------------------------------------//
// MARK: UICollectionViewDataSource
//-------------------------------------------------------------------------//

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"Channels count: %i", [channels count]);

    return [channels count];
    //return 8;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    Channel *channel = channels[indexPath.row];

    ChannelCell *cell = (ChannelCell *)[collectionView dequeueReusableCellWithReuseIdentifier: @"ChannelCell" forIndexPath: indexPath];
    [cell.thumbnail setImageWithUrl: channel.thumbnailUrl];
    [cell.channelTitle setText: channel.channelTitle];
    //[cell.channelTitle setText: @"Test Cell"];

    return cell;
}

//-------------------------------------------------------------------------//
// MARK: Memory Warning
//-------------------------------------------------------------------------//

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

0 个答案:

没有答案