新手的UICollectionView设置帮助

时间:2013-01-21 00:45:55

标签: ios6 xcode4.5 uicollectionview

首先,如果答案非常明显但我对iOS开发并不陌生(这就是我试图编写的第一个应用程序,即使它只是为了我玩:P),所以道歉是这样的概率我的问题相当小,很高。

代码(包含xcode项目的zip文件)

http://www.mediafire.com/?p55xw0q2dwwwwvm

我保证那里没有别的东西有害:)。

问题:

我正在关注:

http://www.techotopia.com/index.php/An_iPhone_iOS_6_Storyboard-based_Collection_View_Tutorial

我正在尝试制作一个UICollectionView。我下载了“测试应用程序”部分。我把自己的旋转放在指南的几个部分上:

  1. 而不是使用不同的图片,我只使用1张图片,底部有一个标签,上面只有一个数字。该数字是动态设置的。是的,我可以在没有我正在使用的数组的情况下实现它,但是如果我将来扩展这个项目的范围,那么拥有那个数组会有所帮助。

  2. 我在viewDidLoad中添加了代码,将视图布局设置为流布局。这是为了将来的防护再次完成,因为一旦我以基本形式工作,我想要使用格式化,这将需要我子类化flowlayout。

  3. 代码编译没有错误,但屏幕上没有任何内容。我已经大约一两个小时检查了我的能力,但我没有做任何改变。我的控制器类是一个简单的尝试,只是让集合视图显示在屏幕上,而我的单元类是一个只有一个imageview和标签的裸骨单元。

    感谢您为此工作提供的任何帮助!

    TL;博士 只要看看粗体的东西。为方便起见,下面提供了代码:

    MyCollectionViewController.m

    #import "MyCollectionViewController.h"
    
        @interface MyCollectionViewController ()
        @property (nonatomic, strong) NSMutableArray *dataArray;
        @end
    
        @implementation MyCollectionViewController
    
        @synthesize dataArray = _dataArray;
    
        - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
        {
            self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
            if (self) {
                // Custom initialization
            }
            return self;
        }
    
        - (void)viewDidLoad
        {
            [super viewDidLoad];
    
            // Create data to display
            for(int i = 0; i < 50; i++){
                [self.dataArray addObject:[NSString stringWithFormat:@"%d", i]];
            }
    
            // Configure Layout
            UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
            [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
            [self.collectionView setCollectionViewLayout:flowLayout];
    
        }
    
        - (void)didReceiveMemoryWarning
        {
            [super didReceiveMemoryWarning];
            // Dispose of any resources that can be recreated.
        }
    
        #pragma mark -
        #pragma mark UICollectionViewDataSource
    
        -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
        {
            return 1;
        }
    
        -(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
        {
            return [self.dataArray count];
        }
    
        -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
        {
            MyCollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
            int row = [indexPath row];
            UIImage* image = [UIImage imageNamed:@"200px-AND_ANSI"];
            myCell.cellTitle = [self.dataArray objectAtIndex:row];
            myCell.cellImageView.image = image;
            myCell.backgroundColor = UIColor.whiteColor;
    
            return myCell;
        }
    

    MyCollectionViewcontroller.h

    #import <UIKit/UIKit.h>
    #import "MyCollectionViewCell.h"
    
    @interface MyCollectionViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
    
    @end
    

    MyCollectionViewCell.h

    #import <UIKit/UIKit.h>
    
    @interface MyCollectionViewCell : UICollectionViewCell
    @property (strong, nonatomic) IBOutlet UIImageView *cellImageView;
    @property (strong, nonatomic) IBOutlet UILabel *cellTitle;
    
    @end
    

    MyCollectionViewCell.m

    #import "MyCollectionViewCell.h"
    
    @implementation MyCollectionViewCell
    
    @synthesize cellImageView = _cellimageView;
    @synthesize cellTitle = _cellTitle;
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    @end
    

1 个答案:

答案 0 :(得分:1)

self.dataArray无法在您的代码中进行分配/初始化,因此等于nil。允许向nil发送消息,但无效,因此即使在

之后也是如此
for(int i = 0; i < 50; i++){
    [self.dataArray addObject:[NSString stringWithFormat:@"%d", i]];
}

self.dataArray仍为nil[self.dataArray count]返回0

您必须使用

分配和初始化数组
self.dataArray = [[NSMutableArray alloc] init];

正确的位置是视图控制器的一些initXXX方法。但是,如果视图控制器是从故事板文件中实例化的,则不会调用initWithNibName:,而是必须使用initWithCoder:

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.dataArray = [NSMutableArray array];
    }
    return self;
}
相关问题