使用xib方式自定义cell
tableView使用代码方式自定义cell
collectionView使用代码方式自定义cell
原型cell
1.创建一个自定义的cell类,并继承UITableViewCell
2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
4.拖线生成属性
5.给cell定义一个模型属性,并重写该模型属性的set方法
![计算机生成了可选文字:+ (instancetype)goodsCellWithTableView: (UI TableView *)tableView { [tableView dequeueReusableCellWithIdentifier:ID] ; HMGoodsCell *cell 1 ce = [ [ [NSBundle mainBundle] owner: nil options:nil] lastObject] ; cell return cell;]()
原型cell的方式自定义cell:
1.创建一个自定义的cell类,并继承UITableViewCell
2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
4.拖线生成属性
5.给cell定义一个模型属性,并重写该模型属性的set方法
![计算机生成了可选文字:(UITableViewCeII *)tableView: (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath * ) indexPath { / / 1. 获 取 模 型 数 据 self apps[indexPath. row HMApp *app / / 2 . 创 建 Cell static NSString *ID = @"app_cell" [tableView dequeueReusableCellWithIdentifier:ID]; HMAppCeII *cell / / 为 每 个 Ce 且 指 定 一 个 代 理 cell.delegate self, / / 3 · 把 模 型 数 据 设 置 给 cell cell. app app; / / 4 · 返 回 Cell return cell; 这 里 只 需 要 在 缓 存 池 中 查 找 相 同 旧 的 ce 丨 丨 就 OK 了 , 如 果 缓 存 池 中 没 有 , 系 统 就 根 据 原 型 c 酬 重 新 创 建 一 个 , 不 需 要 我 们 手 动 创]()
原型cell和xib创建cell的不同点:
xib创建cell比原型cell多一步(在缓存池中没有找到cell,xib方式需要自己手动创建一个cell,而原型cell不需要手动创建,系统会自己根据原型cell的模板来创建)
tableView代码方式自定义cell:
1.创建一个自定义的cell类,并继承UITableViewCell
2.手动在类扩展中定义需要的控件属性
3.重写initWithStyle方法,在方法里面创建子控件,并赋值给类扩展中的属性(这里就相当于拖线)
4.给cell定义一个模型属性,并重写该模型属性的set方法
![计算机生成了可选文字:#pragma mark - + (instancetype)weiboCellWithTableView: (UI TableView *)tableView { static NSString *ID — @"weibo_cell"; - [tableView dequeueReusableCellWithIdentifier. HMWeiboCell *cell - (cell =- nil) { if [ [HMWeiboCell al loc] reuseldentifier: ID] ; cell - cell; return]()
tableView的团购案例:
xib方式创建单元格
![计算机生成了可选文字:> 14 Placeholders File's Owner First Responder goods_cell Views Image View x HMGoodsCell.xib goods_cell Label Label e View Cell Style Identifier Selection Accessory Editing Acc. Indentation Label Custom goods_cell Default None None Level 10 Width O Indent While Editing Shows Re-order Controls Separator Default Insets]()
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMGoods; @interface HMGoodsCell . UlTab1eViewCe11 UlTableViewCell @property (nonatomic, strong) HMGoods *goods ; + (instancetype)goodsCellWithTableView: (UI TableView *)tableView; @end]()
![计算机生成了可选文字:#pragma mark - + (instancetype)goodsCellWithTableView: (UI TableView *)tableView { static NSString *ID = *cell = [tableView dequeueReusableCellWithIdentifier:ID] ; HMGoodsCell if (cell nil) { = [ [ [NSBundle mainBundle] owner: nil cell options: nil] lastObject]; return cell;]()
xib方式创建footerView(headerView和footerView方式一样):
![计算机生成了可选文字:Views HMGoodsFooterView.xib Goods Footer View Placeholders File's Owner First Responder Goods Footer View Custom Class Module Identity Restoration ID HMGoodsFooterView None User Defined Runtime Attributes Key Path Type *RID Document Value Title Item]()
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMGoodsFooterView; @protocol HMGoodsFooterViewDelegate <NS0bject> - (void)goodsFooterViewDidClickLoadMoreButton: (HMGoodsFooterView *)footerView; @end UlView @interface HMGoodsFooterView : UIView @property (nonatomic, weak) id<HMGoodsFooterViewDelegate > delegate; + (instancetype)goodsFooterView; @end]()
![计算机生成了可选文字:+ (instancetype)goodsFooterView { *footerView = [ [ [NSBundle mainBundle] HMGoodsFooterView loadNibNamed:@"HMGoodsFooterView'I owner: nil options: nil] lastObject] ; return footerView•]()
![计算机生成了可选文字:#pragma mark - viewDidLoad - (void)viewDidLoad { [super viewDidLoad] ; self. tableView. rowHeight // jaäEfif00terView HMGoodsFoote rV iew *foote rV iew self. tableView. tableFooterView = footerView; // view HMGoodsHeade rV iew *heade rV iew self. tableView. tableHeaderView = headerView; iMiü#fifiit EheaderView tableView* [HMGoodsFooterView goodsFooterView] ; [HMGoodsHeaderView goodsHeaderView] ;]()
tableView的微博案例(QQ聊天布局案例和它相同):
代码方式创建单元格:
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMWeiboFrame; @interface HMWeiboCell . UlTab1eViewCe11 UlTaDleViewCell @property (nonatomic, strong) HMWeiboFrame *weiboFrame; + (instancetype)weiboCellWithTableView: (UI TableView *)tableView; @end]()
![计算机生成了可选文字:#pragma mark - + (instancetype)weiboCellWithTableView: (UI TableView *)tableView { static NSString *ID = *cell = [tableView dequeueReusableCellWithIdentifier:ID] ; HMWeiboCell if (cell nil) { [ [HMWeiboCell al loc] initWithStyle: ITableViewCellStyleDefault cell reuseldentifier: ID] ; return cell; #pragma mark - iMjä initWithStyIe - (instancetype) initWithStyle: (UI TableViewCellStyle)style reuseldentifier: (NSString reuseldentifier { 1 se super Inl 1 y e:s y e reuse UllmageView *imgViewIcon = [ [UllmageView en 1 ler: reuse en 1 ler al loc] init] ;]()
tableView的QQ好友列表案例:
代码方式创建组头、组尾:
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMGroup; @interface HMGroupHeaderView : UI TableViewHeaderFooterView @property (nonatomic, strong) HMGroup *g roup ; + (instancetype)groupHeaderViewWithTableView: (UI Tabl @end]()
![计算机生成了可选文字:+ (instancetype)groupHeaderViewWithTableView: (UI TableView *)tableView { static NSString *ID = @"group_header_view"; *headerVw = [tableView HMG roupHeade rV iew dequeueReusableHeaderFooterViewWithIden inltWithReuseI if (headerVw nil) { // initWithReuseIdentifier headerVw = [[HMGroupHeaderView alloc] initWithReuseIdentifier: D] ; return headerVw; // initWithReuseIdentifier - (instancetype) initWithReuseIdentifier: (NSString reuseldentifier { [super initWithReuseIdentifier: reuseldentifier]) { if (self - // header]()
![计算机生成了可选文字:// äEXGÆfiheader - (UIView *)tableView: (UI TableView *)tableVie viewForHeaderInSection: section { i*tabIeView = self.groups[section] ; HMGroup roup // 2. view NSInteger) *headerVw = [HMGroupHeaderView groupHeaderViewWithTableView: HMG roupHeade rV iew tableView] ; // header view // fEZ: GheaderVw, Gtable view // ißLGtable view2äjåN}Etable viewä9fiiQN, viewfif rame // FfiL\Å, view —FEfiframeÆO, header viewfio heade rVw. group = group; // 4. jfi@header view return headerVw;]()
tableView的应用管理案例:
原型cell方式创建单元格:
![计算机生成了可选文字:Views M...rd Table View Controller Scene Table View Controller v app_cell app_cell < Table View ontent View First Respon Exit Storyboard Entry Point Prototype Cells Label Image Label w Cell Style Custom Identifier app_cell Accessory None Editing Acc. None Indentation Level Width O Indent While Editing 000 10 Table View Prototype Content]()
![计算机生成了可选文字:#import <UIKit/UIKit.h> @class HMApp, HMAppCell; @protocol HMAppCellDelegate 4SObject> - (void)appCellDidClickDownloadButton: (HMAppCell appCell; @end @interface HMAppCe11 . @property (nonatomic, @property (nonatomic, @end UlTableViewCell UlTab1eViewCe11 weak) id<HMAppCellDelegate > delegate; strong) HMApp *app;]()
![计算机生成了可选文字:- (UI TableViewCell *)tableView: (UI TableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *ID = = [tableView dequeueReusableCellWithIdentifier:ID] ; HMAppCe11 *cell cell. delegate = self; cell. app - app; // 4. jÉ@Cell return cell;]()
collectionView的案例:
纯代码自定义cell:
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMAppCollectionViewCell . UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end]()
![计算机生成了可选文字:// initWithFrame - (instancetype) initWithFrame: (CGRect)frame { [super initWithFrame:frame]) { if (self - = [ [UllmageView al loc] UllmageView *imgViewIcon [self. contentView addSubview: imgViewIcon] ; self . imgViewIcon imgV iewlcon ; // label = [[UILabel al loc] init] ; UILabel *lblName lblName. font = [UIFont systemFont0fSize: 12] ; lblName. textAlignment = NSTextAlignmentCenter; [self. contentView addSubview: lblName] ; self . lblName = lblName; // button = [[UIButton al loc] init] ; UIButton *button init] ; [button setBackgroundImage: [UI Image imageNamed:@Ibuttongreen"] forState: UICont rolStateNormal] ;]()
下面这行代码会执行initWithFrame方法
HMAppCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
![计算机生成了可选文字:static NSString * const ID - void viewDidLoad [super viewDidLoad] ; [self. collectionView registerClass: [HMAppCollectionViewCell class forCellWithReuseIdent1f1er // iQNCollection = [UIColor whiteColor] ; self. collectionView. backg roundColor - (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath { = self. apps [indexPath. item] ; *model HMApp cell HMAppCol lectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jä@cell return cell;]()
xib方式自定义cell:
![计算机生成了可选文字:> 14 Placeholders File's Owner First Responder app_cell Img Vie on Lbl Name @Btn Download Constraints Views HMAppCollectionViewCell.xib äEXficeII Ima Label app_cell eusable View Identifier app_cell View Mode Center Semantic Unspecified Tag Interaction O User Interaction Enabled O Multiple Touch Alpha Background Default Tint Default 1 Drawing Opaque Hidden O Clears Graphics Context O Clip Subviews O Autoresize Subviews Stretching x 1 1]()
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMAppCollectionViewCell . UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end]()
![计算机生成了可选文字:static NSString * const ID - (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath {l = self. apps [indexPath. item] ; *model HMApp // 2. @lJECe11 HMAppCol lect ionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jä@cell return cell; #pragma mark - viewDidLoad - (void)viewDidLoad { [super viewDidLoad] ; iMiä registerNib UINib *nib = [UINib nibWithNibName:@"HMAppCollectionViewCell" bundle: nil] ; [self. collectionView registerNib: nib forCellWithReuseIdentifier: = [UIColor whiteColor] ; self. collectionView. backg roundColor]()
原型cell方式自定义cell:
![计算机生成了可选文字:> 14 ä EXCell Collection View app_cell O View Controller Scene Collection View app_cell Colle Vie... First Respon er Exit Storyboard Entry Poi... Collection View Contr... View Controller Scene > coil View Ima Label Identifier app_cell Mode Center Semantic Unspecified Tag Interaction O User Interaction Enabled O Multiple Touch Alpha Background Default Tint Default 1 Drawing Opaque Hidden O Clears Graphics Context O Clip Subviews O Autoresize Subviews Stretching x 1 1]()
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMCollectionViewCell UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end]()
![计算机生成了可选文字:- (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath { = self. apps [indexPath. item] ; *model HMApp // (prototype cell) static NSString *ID = = [collectionView HMCollectionV iewCell *cell dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jÉ@Cell return cell;]()
代码方式创建组头、组尾:
![计算机生成了可选文字:#import <UIKit/UIKit. @class HMPhoto; // UICollectionReusa @interface HMSectionHeaderView : UICollectionReusableView @property (nonatomic, strong) HMPhoto *photo ; @end]()
![计算机生成了可选文字:- (instancetype) initWithFrame: (CGRect)frame { [super initWithFrame:frame]) { if (self = [[UILabel al loc] init] ; UILabel *lblTitle lblTitle. font = [UIFont systemFontOfSize: 12] ; lblTitle. textAlignment = NSTextAlignmentCenter; [self addSubview: lblTitle] ; self . lblTitle = lblTitle; self. backg roundColor return self; = [UIColor]()
![计算机生成了可选文字:- (void)viewDidLoad { [super viewDidLoad] ; [self. collectionView registerClass: [HMPhotoCollectionViewCell class] forCellWithReuseIdentifier: CGFloat itemW = self. collectionView. bounds. size. width; CGFloat itemH = itemW; = CGSizeMake( itemW, itemH) ; self. flowLayout . itemSize // view [self. collectionView registerClass: [HMSectionHeaderView class] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"header"] ; // Efooter View [self. collectionView registerClass: [HMSectionFooterView class] forSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"] ; // i±MGÆfiheader footerfiX/J\ self. flowLayout . heade rRefe renceSize self. flowLayout. footerReferenceSize = CGSizeMake(0, = CGSizeMake(0, 49) ; 49) ;]()
tableView使用代码方式自定义cell
collectionView使用代码方式自定义cell
原型cell
静态cell
1.创建一个自定义的cell类,并继承UITableViewCell
2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
4.拖线生成属性
5.给cell定义一个模型属性,并重写该模型属性的set方法
原型cell的方式自定义cell:
1.创建一个自定义的cell类,并继承UITableViewCell
2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
4.拖线生成属性
5.给cell定义一个模型属性,并重写该模型属性的set方法
原型cell和xib创建cell的不同点:
xib创建cell比原型cell多一步(在缓存池中没有找到cell,xib方式需要自己手动创建一个cell,而原型cell不需要手动创建,系统会自己根据原型cell的模板来创建)
tableView代码方式自定义cell:
1.创建一个自定义的cell类,并继承UITableViewCell
2.手动在类扩展中定义需要的控件属性
3.重写initWithStyle方法,在方法里面创建子控件,并赋值给类扩展中的属性(这里就相当于拖线)
4.给cell定义一个模型属性,并重写该模型属性的set方法
tableView的团购案例:
xib方式创建单元格
xib方式创建footerView(headerView和footerView方式一样):
tableView的微博案例(QQ聊天布局案例和它相同):
代码方式创建单元格:
tableView的QQ好友列表案例:
代码方式创建组头、组尾:
tableView的应用管理案例:
原型cell方式创建单元格:
collectionView的案例:
纯代码自定义cell:
下面这行代码会执行initWithFrame方法
HMAppCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
xib方式自定义cell:
原型cell方式自定义cell:
代码方式创建组头、组尾: