从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可省略空值判断的步骤。

时间: 2025-01-02 03:36:53

从xib 创建 collectionViewCell的相关文章

iOS UICollectionView的使用(用storyboard和xib创建)

1. 在storyboard中,拖出1个UICollectionViewController 2. 新建file--Cocoa Touch Class,继承自UICollectionViewController,假设名字是CollectionDemo 3. 在storyboard, 把刚才拖出来的UICollectionViewController的class改成CollectionDemo 4. 在 CollectionDemo.m 中实现,数据源方法 - (NSInteger)collect

用XIB创建UIView

用XIB创建UIView  一般创建UIView时,同时创建XIB这个选项是不允许选择的,但是可以自己创建一个XIB,与类关联起来 step1 创建XIB,类名最好与类的名称相同,易区分,关联类     注: 要改变UIView的大小,将UIView的属性size选择为freedom step 2 在XIB中添加控件,将属性添加到类中,并在类中写一个初始化instanceView类方法 HeadView.m + (HeadView *)instanceView { NSArray *nibVie

IOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用

一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // 返回第component有多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

用Xib创建控制器

注:用Xib创建控制器作为视图展示时,系统自己会自动去找之相似的xib文件,即(按照优先度从高到低)先找VSXibViewControll.xib文件,再找VSXibView.xib文件,再找与之相关多其他xib文件. 步骤一:xib文件名称与控制器类的名称无关. 1.创建一个VSXibViewControll.h, VSXibViewControll.m文件,再创建一个vsstar的xib文件. 2.点击VSstar.xib文件,给大视图加一个颜色,之后选择File`s Owner,选择右侧"

[iOS]swift之UITableView添加通过xib创建的headerView坑爹问题

情景是这样的,我UITableView添加了一个HeaderView,这个HeaderView是通过xib创建,是UIView.出来的结果却出乎意料,UITableView的Cell最顶部的几个被HeaderView给遮挡了---我勒个去--神马情况???!!! 于是哥通过看层次结构,发现运行出来的HeaderView和Cell列表不在同一层次,理应是同一层才对呀!!!!于是我用其他xib试试,情况一样,然后改用代码创建 UIView() 类似这种方式,这样就是正常的,HeaderView和Ce

Xcode中不用Storyboard,用纯xib创建TabBar模式视图

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果要开发Tab类型视图的App,在Xcode中可以使用对应的模板 该模板自然使用的是Storyboard那一套东东,为了更清楚的了解xib布局,我们下面不用Storyboard模板来打造一个TabBar视图的App. 第一步:创建Single View App 打开Xcode,选择Single View App模板,创建新项目.然后将项目中的所有storyb

登陆界面 键盘工具栏的xib创建

1 // 2 // LXKeyboardTool.h 3 // 注册界面 4 // 5 // Created by 刘羽 on 16/1/5. 6 // Copyright © 2016年 LX. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 typedef enum{ 12 keyboardItemTypeNext, 13 keyboardItemTypePrevious, 14 keyboardItemTypeDon

通过Xib创建 UICollectionView 和自定义UICollectionViewCell

1.在控制器的viewDidLoad方法中添加代码 CGFloat itemWidth = (kScreenW - kSpacingW * 3) / 2; NSLog(@"itemWidth == %f",itemWidth); CGFloat itemHeight = itemWidth * 0.75 + 71; NSLog(@"itemWidth == %f",itemHeight); UICollectionViewFlowLayout*layout = [[

114自定义 UITableViewCell(扩展知识:如何使用xib创建自定义的表格视图单元格 KMTableViewCell)

关键操作: 效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataList; 5 @property (strong, nonatomic) NSMutableArray *mArrImageList; 6 7 @end ViewCo