0

IOS 和 Objective C 的新手,一直在做几个教程,添加按钮、文本字段等。在 stackoverflow 上看了很多,这非常有帮助:) 现在我正在尝试在框架内创建一个集合视图,然后在它下面放一个按钮. 我可以显示一个按钮,并且我已定义要显示的集合的屏幕上半部分变为黑色,而其余部分变为白色,如预期的那样。目前,该系列应该带有一个标签,告诉我手机号码,非常基本。不确定我的视图设置是否错误,或者我的 CellViews 是否有问题。我创建了一个 mainView 控制器,在其中定义了我的按钮和集合视图,如下所示: MainViewController.h:

@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;
}

任何帮助将不胜感激。

4

1 回答 1

0

我不知道为什么您的收藏没有显示任何内容,但是您缺少 UICollectionViewDataSource 中的 numberOfSectionsInCollectionView: 方法。集合视图可能假设节数为 0。

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

另外,您是否为您的 UICollectionView 设置了数据源和委托给自己?

于 2013-07-10T12:43:27.410 回答