注意要 -- 注冊 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可省略空值判斷的步驟。
