從xib 創建 collectionViewCell


注意要 -- 注冊 xib

- (void)awakeFromNib {

    [super awakeFromNib];
    
    UINib *nib = [UINib nibWithNibName:@"MyPurchaseRecordFooterCell" bundle:[NSBundle mainBundle]];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"MyPurchaseRecordId"];
}

 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // 在這里注冊 nib 也可以 // UINib *nib = [UINib nibWithNibName:@"MyPurchaseRecordFooterCell" bundle:[NSBundle mainBundle]]; // [collectionView registerNib:nib forCellWithReuseIdentifier:@"MyPurchaseRecordId"]; MyPurchaseRecordFooterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyPurchaseRecordId" forIndexPath:indexPath]; cell.model = self.collectionModelM[indexPath.item]; return cell; }

 

2.從xib 創建tableViewCell

可以在創建tableview的時候,直接 注冊nib

self.tableView.backgroundColor = xxxx;
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
這樣你在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath這個方法里,你就可以省下這些代碼:
 
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 
}
而只需要

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

 

3.需不需要注冊?
使用dequeueReuseableCellWithIdentifier:可不注冊,但是必須對獲取回來的cell進行判斷是否為空,若空則手動創建新的cell;
使用dequeueReuseableCellWithIdentifier:forIndexPath:必須注冊,但返回的cell可省略空值判斷的步驟。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM