0

IOS と Objective C を初めて使用し、ボタンやテキストフィールドなどを追加して、いくつかのチュートリアルを行ってきました。stackoverflow をよく見ています。 . ボタンを表示させると、意図したとおり、コレクションを表示するように定義した画面の上半分が黒くなり、残りは白くなります。現在、コレクションにはセル番号を示すラベルが付いているはずなので、非常に基本的です。ビューの設定が間違っているのか、CellViews に何か問題があるのか​​ わかりません。次のように、ボタンとコレクション ビューを定義した mainView コントローラーを作成しました。

@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