2013-07-10 45 views
0

IOS和目标C的新手,一直在做几个教程,添加按钮,textfields等在stackoverflow上看起来很有帮助:)现在我试图在一个框架内创建一个collectionview,然后在它下面放一个按钮。我可以得到一个按钮来显示,并且我已经定义集合的屏幕的上半部分变成黑色,而其余的部分变成白色,如预期的那样。目前收集应该拿出一个标签告诉我细胞的数量,所以非常基本。不知道我是否设置了错误的视图,或者我的CellView有什么问题。 我创建了一个MAINVIEW控制器,在那里我有我的定义按钮,集合视图,就像这样: MainViewController.h:IOS,UICollectionview

@interface MainViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> 

@property(nonatomic, strong) IBOutlet UICollectionView *collectionView; 
@property(nonatomic, strong) IBOutlet UIButton *diaryButton; 

- (id)init; 

我的.m看起来是这样的:

@implementation MainViewController 
@synthesize collectionView; 
@synthesize diaryButton; 

- (id) init { 

self = [super init]; 
if (self) { 

    CGRect buttonFrame = CGRectMake(10, 250, 70, 30); 
    diaryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [diaryButton setFrame:buttonFrame]; 
    [diaryButton setTitle:@"Diary" forState:UIControlStateNormal]; 

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
    layout.itemSize = CGSizeMake(64.0, 64.0); 
    layout.minimumInteritemSpacing = 4; 
    layout.minimumLineSpacing = 4; 
    layout.scrollDirection = UICollectionViewScrollPositionCenteredVertically; 
    layout.sectionInset = UIEdgeInsetsMake(2.0, 2.0, 2.0, 2.0); 
    CGRect collectionFrame = CGRectMake(0, 0, 320, 200); 
    collectionView = [[UICollectionView alloc] initWithFrame:collectionFrame collectionViewLayout:layout]; 


    [self.view addSubview:collectionView]; 
    [self.view addSubview:diaryButton]; 
    //[self.view setBackgroundColor:[UIColor blueColor]]; 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ID"]; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 30; 
} 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath]; 
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds]; 
    label.textAlignment = NSTextAlignmentCenter; 
    label.text = [NSString stringWithFormat:@"%d", indexPath.row]; 
    [cell.contentView addSubview:label]; 
    return cell; 
} 
///////////////////////////////////////////////////////////////////////////////// 
// collection view delegate methods //////////////////////////////////////// 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"cell #%d was selected", indexPath.row); 
} 

我的appdelegate外观像这样:

@synthesize mainVC; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.mainVC = [[MainViewController alloc] init]; 
    [self.window addSubview:mainVC.view]; 
    //self.window.rootViewController = mainVC; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

任何帮助将不胜感激。

+0

不清楚你在问什么,或者你得到的结果有什么问题。你能更清楚地知道你想要发现什么以及你已经做了什么来诊断问题吗? – Caleb

回答

0

我不知道为什么你的集合没有显示任何内容,但是你缺少来自UICollectionViewDataSource的numberOfSectionsInCollectionView:方法。该集合视图可能会假设部分的数量为0。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewDataSource_protocol/Reference/Reference.html

另外,你设定的数据源和委托自我为您UICollectionView?

+0

numberOfSectionsInCollectionView:方法似乎默认为1,但无论如何我现在定义它。 如何设置数据源?我是否告诉UICollectionView它的数据源是什么? (这是主控制器,我猜)。没有找到任何可以设置数据源的方法。我误解你了吗? –

+1

找到了setDatasource方法。不知道我第一次错过了它,它工作:) 非常感谢! –

+0

不错,很高兴它有帮助。如果你不想在代码中设置它,你可以通过按住控件来设置委托和数据源,然后点击并从UICollectionView拖动鼠标到MainViewController并释放。然后检查数据源。代表也是如此。如果答案有帮助,不要忘记标记为答案:-) –