UIcollectionView控制器中没有任何东西

时间:2014-03-24 11:11:48

标签: ios objective-c

我在Uinavigationviewcontroller之后的第一个应用程序视图中有UIcollectionView,就像这样:

enter image description here

这是我的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

现在问题不在于我的&#34; cellForItemAtIndexPath&#34;或&#34; numberOfItemsInSection&#34;甚至&#34; viewDidLoad&#34;被调用并且模拟器上的输出是黑屏。

这是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;
}

2 个答案:

答案 0 :(得分:2)

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

做到的

答案 1 :(得分:1)

故事板上是否有标识符?您可以尝试添加

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];

在viewDidLoad

由于您的RootViewController继承了UICollectionViewController,因此您根本不需要添加UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout