从xib 创建 collectionViewCell
生活随笔
收集整理的這篇文章主要介紹了
从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可省略空值判斷的步驟。
?
轉載于:https://www.cnblogs.com/yangzhifan/p/5594829.html
總結
以上是生活随笔為你收集整理的从xib 创建 collectionViewCell的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何获取程序返回值,退出码,错误码
- 下一篇: 20145228《Java程序设计》课程