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;
}
任何幫助將不勝感激。
不清楚你在問什麼,或者你得到的結果有什麼問題。你能更清楚地知道你想要發現什麼以及你已經做了什麼來診斷問題嗎? – Caleb