在故事板中的Uinavigationviewcontroller之后,我在应用程序的第一个视图中就有UIcollectionView,如下所示:



这是我的RootViewController.h

#import <UIKit/UIKit.h>
@interface RootViewController : UICollectionViewController<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) NSArray *entries;
 @end


和我的RootViewController.m:

#import "RootViewController.h"
#import "AppRecord.h"
#import "Cell.h"
#define kCustomRowCount     7


@interface RootViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
// the set of IconDownloader objects for each app
@property (nonatomic, strong) NSMutableDictionary *imageDownloadsInProgress;
@end


@implementation RootViewController

#pragma mark

// -------------------------------------------------------------------------------
//  viewDidLoad
// -------------------------------------------------------------------------------
- (void)viewDidLoad
{
 NSLog(@"inside class");
    [super viewDidLoad];
   // self.title = @"My Title";

    //self.collectionView.delegate = self;
    //self.collectionView.dataSource=self;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSUInteger count = [self.entries count];
    NSLog(@"count: %lu", (unsigned long)count);

    if (count == 0)
    {
        return kCustomRowCount;
    }
    return count;
}


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

    NSLog(@"inside cell");
    AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
    UIImage *truckImage = [[UIImage alloc] init];
    truckImage = [UIImage imageNamed:@"Default.png"];
    cell.imageView.image = truckImage;
    return cell;
}

@end


现在的问题是我的“ cellForItemAtIndexPath”或“ numberOfItemsInSection”甚至“ viewDidLoad”都没有被调用,并且模拟器上的输出是黑屏。

这是我的AppDelegate类的重载部分:

    __block ParseOperation *weakParser = parser;

    parser.completionBlock = ^(void) {
        if (weakParser.appRecordList) {
            dispatch_async(dispatch_get_main_queue(), ^{
                RootViewController *rootViewController = (RootViewController*)[(UINavigationController*)self.window.rootViewController topViewController];

                rootViewController.entries = weakParser.appRecordList;
                if(weakParser.appRecordList != nil)
                    NSLog(@"weakParser.appRecordList is Not nill");
                [rootViewController.collectionView reloadItemsAtIndexPaths:[rootViewController.collectionView indexPathsForVisibleItems]];
                [rootViewController.collectionView reloadData];
            });
        }

        self.queue = nil;
    };

    [self.queue addOperation:parser];
    self.appListData = nil;
}

最佳答案

您是否为集合视图添加了委托和数据源连接?通常,这是最常见的错误。他们在代码中被注释掉了,我想你是通过Storyboard完成的

09-29 20:49