xibの使用

6033 ワード

xibを使用してCollectionViewを作成する例を示します.
1.まずViewControllerを削除する.h,ViewController.mとStoryBoardは、Deployment InfoでMain Interfaceをクリアします.
2.プログラムが実行されます.画面は黒です.つまり、現在表示されていない内容です.UIViewControllerを作成し、このコントローラにcollection viewを追加して目的を達成します.
3.CollectionViewControllerという名前のUIViewControllerを作成し、対応するxibファイルの作成をチェックします.
4.CollectionViewControlの内容を表示する必要があります.
AppDelegateでh中:
#import 

@class CollectionViewController;

@interface AppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) CollectionViewController *collectionVC;

@end

AppDelegateでm:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.collectionVC = [[CollectionViewController alloc] initWithNibName:@"CollectionViewController" bundle:nil];
    self.window.rootViewController = self.collectionVC;
    [self.window makeKeyAndVisible];
    return YES;
}

collectioncVCをselfに設定します.WindowsのrootViewController、このときWindowsで最初に表示されるのがcollectionVCのviewの内容です.
プログラムを実行すると、空白が見つかりました.これは、私たちが最初に作成したのはUIViewControllerで、そのviewは白いからです.labelをドラッグしてプログラムを実行すると、labelが画面に表示されます.
collection viewを表示する必要があります.だからCollectionViewをドラッグしましたこのときプログラムを実行すると、画面がまた黒くなっています.焦らないで.CollectionViewのデフォルトのbackgroundColorは黒です.コントロール-dragを使用して、CollectionViewをCollectionViewのoutletに設定し、「collectionView」と名前を付けます.今、私たちのCollectionViewControllerはこのCollectionViewを管理しています.CollectionViewControllerをcollectionViewのプロキシとデータソースに設定し、データソースメソッドを実装します.
ここには小さな例があります.
#import "CollectionViewController.h"

@interface CollectionViewController () 
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *colorsArray;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell Identifier"];
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    const int count = 100;
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:count];
    for (int i = 0; i < count; i++) {
        CGFloat red = (arc4random()%255)/255.0f;
        CGFloat green = (arc4random()%255)/255.0f;
        CGFloat blue = (arc4random()%255)/255.0f;
        [tempArray addObject:[UIColor colorWithRed:red green:green blue:blue alpha:1.0f]];
    }
    self.colorsArray = [NSArray arrayWithArray:tempArray];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.colorsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell Identifier" forIndexPath:indexPath];
    cell.backgroundColor = self.colorsArray[indexPath.row];
    return cell;
}

@end

まず私たちはselfにいます.collectionViewにはUICOLLectionViewCellというクラスが登録されており、identifierが「Cell Identifiier」に設定されています.これにより、次のd e q u e R e u s a b l e CellWithReuseIdentifierのときに、現在登録されているこのようなcellを繰り返し利用することができます.if(cell=nil){......}というコードを省く.StoryBoardを使用する場合、このようにコードを使用してCellのタイプとidentifierを設定する必要はありません.可視化インタフェースではすでに設定されているので、d e q u e R u s ableCellWithReuseIdentifierを直接使用して、可視化インタフェースで設定した対応するcellを繰り返し利用します.
ここで私はこれまでうっかりしていたので、red,green,blueを求めていたとき、255.0 fを255と書いてしまいました.これでred,green,blueはずっと0なので、配列の中ではすべてのオブジェクトが黒で、collectionを加えるともともと黒なので、それを加えると黒になります.以前はxibの使用でエラーがあったと思っていましたが、エラーは見つかりませんでした.のあとでcollectionView自体が黒であることを知ったし、私が色を手に入れたときのうっかりしたこともあって、これからは気をつけなければなりません.
ここでもう1つのケースは、CollectionViewControllerを作成するときに、対応するxibの作成にチェックマークが付いていないことです.方法は次のとおりです.
1.xibファイルを作成します.
2.xibファイルに対応するタイプのコントロールをドラッグします.私たちのCollectionViewControllerがUIViewControllerである場合、私たちはUIViewをドラッグします.
3.xibファイルのFile's OwnerをCollectionViewControlに設定します.
4.ドラッグしたviewをCollectionViewControlのviewに設定します.(control-drag)
次は前に言ったのと同じです.要約すると、xibの作成が開始されていない場合は、上記の手順に従ってCollectionViewControllerと関連付ける必要があります(注意タイプに対応してください).関連付けが完了すると、開始時にxibが直接作成されるのと同じです.開始時に対応するxibファイルを直接作成することは、自動的に関連付けを完了することに相当し、私たちの作業を省くことができます.
ついでにメモします.
私たちがcellを使用するときに使用するのは、システムのデフォルトのUItableViewCell/UIcollectionCellなので、登録するとき:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell Identifier"];

しかし、多くの場合、システムが持っているcellは私たちのニーズを満たすことができません.xibでcellを作成し、繰り返し利用したいと思います.
1.xibファイルを作成します.UIcollectionViewCellにドラッグして、目的の内容に設定します.
2.CollectionViewController.mのviewDidLoadメソッドで[self.collectionViewregisterClass:[UICOLLectionViewCell class]forCellWithReuseIdentifiier:@"Cell Identifiier";私たちはもうシステムが持っているcellを使いたくないからです.次の2行のコードを追加します.
UINib *cellNib = [UINib nibWithNibName:@"CustomCollectionCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"Cell Identifier"];

私たちはcollectioncVCを作成するときに、直接通過することができます.
self.collectionVC = [[CollectionViewController alloc] initWithNibName:@"CollectionViewController" bundle:nil];

をクリックしてxibをViewControllerに直接関連付けます.ここではCellを作成し、ViewControllerではないので、上記の方法でまずxibを取得し、collectionViewにこのxib(cell)を登録し、identifierを再利用するために設定する必要があります.
今プログラムを実行していると、カスタムcellを登録していますが、まだ使っていないため、以前と変わらないことがわかります.
cellクラスを作成する必要があります
3.「CustomCollectionViewCell」という名前のUIcollectionViewCellを作成します.CustomCollectionCell.xibのClassは、作成したばかりのCustomCollectionViewCellに設定されます(タイプは一致します).
インタフェースに表示されるのは、カスタマイズしたcellです.