xibで作ったUICollectionViewCellのカスタムクラスを配置する
1. file > new > file > UserInterface でviewを選択、ファイル名をmyCell.xib
ラベルをUICollectionViewCellの上に配置する
2. 同じ名前の.hと.mファイルを作成する
3. myCell.xibのFIle's ownerのCustom ClassをmyCellに
viewを選択して、identifierをmyCellに
Custom CalssもmyCellに
myCellにプロパティを記述
myCell.h
#importmyCell.m@interface myCell : UICollectionViewCell @property (strong, nonatomic) IBOutlet UILabel *cellLabel; @end 
 
#import "myCell.h"
@implementation myCell
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)awakeFromNib
{
    NSLog(@"bundleLoader waked");
}
@end
inspectorのoutletから用意したプロパティとxib上のラベルをつなぐ
このやりかたでないと、
とエラーが...
5. あとはcollectionViewController.mで
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"myCell";
    static BOOL nibCellLoaded = NO;
    
    if(!nibCellLoaded){
        UINib *nib = [UINib nibWithNibName:@"myCell" bundle:nil];
        [collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
        nibCellLoaded = YES;
    }
    
    
    myCell *cell = (OBWeather*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;
}
最後に思ったけど、
クラス名とidentifierって一緒じゃない方がいいのかなあ


 
0 件のコメント:
コメントを投稿