UITableViewCell
的子类,好比XMGTgCell@interface XMGTgCell : UITableViewCell @end
-initWithStyle:reuseIdentifier:
方法
/** * 在这个方法中添加全部的子控件 */ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // ...... } return self; }
-layoutSubviews
方法
[super layoutSubviews]
/** * 在这个方法中计算全部子控件的frame */ - (void)layoutSubviews { [super layoutSubviews]; // ...... }
@class XMGTg; @interface XMGTgCell : UITableViewCell /** 团购模型数据 */ @property (nonatomic, strong) XMGTg *tg; @end
- (void)setTg:(XMGTg *)tg { _tg = tg; // ....... }
定义模型的时候,能够将模型所作的事情也同时定义在模型中,用一个block来存储。缓存
#import <Foundation/Foundation.h> //block typedef void (^rowBaseSelected)(); @interface rowBase : NSObject /**image*/ @property (nonatomic, copy) NSString *icon; /**text*/ @property (nonatomic, copy) NSString *title; /**block*/ @property (nonatomic, copy) rowBaseSelected rowBaseSelected; +(instancetype)initWithIcon:(NSString *)icon title:(NSString *)title; @end
这样在须要的地方,能够设置模型的操做框架
row1.rowBaseSelected = ^(){ OneController *oneController = [[OneController alloc]init]; [self.navigationController pushViewController:oneController animated:YES]; };
在须要执行的地方函数
if (rowBase.rowBaseSelected) { rowBase.rowBaseSelected(); }
给cell一个快速的建立方法字体
+ (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"setting"; YLSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[YLSettingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } return cell; }
其中:ui
cell = [[YLSettingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
这一句代码中,要想实现多态的使用(假如当前cell有多个子类,建立的时候方便),要改为这样atom
cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
[self.tableView registerClass:[XMGTgCell class] forCellReuseIdentifier:ID];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 访问缓存池 XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.tg = self.tgs[indexPath.row]; return cell; }
UITableViewCell
的子类,好比XMGTgCell@interface XMGTgCell : UITableViewCell @end
@interface XMGTgCell() @property (weak, nonatomic) IBOutlet UIImageView *iconImageView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *buyCountLabel; @end
@class XMGTg; @interface XMGTgCell : UITableViewCell /** 团购模型数据 */ @property (nonatomic, strong) XMGTg *tg; @end
- (void)setTg:(XMGTg *)tg { _tg = tg; // ....... }
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 访问缓存池 XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.tg = self.tgs[indexPath.row]; return cell; }
UITableViewCell
的子类,好比XMGTgCell@interface XMGTgCell : UITableViewCell @end
@interface XMGTgCell() @property (weak, nonatomic) IBOutlet UIImageView *iconImageView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *buyCountLabel; @end
@class XMGTg; @interface XMGTgCell : UITableViewCell /** 团购模型数据 */ @property (nonatomic, strong) XMGTg *tg; @end
- (void)setTg:(XMGTg *)tg { _tg = tg; // ....... }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"tg"; // 访问缓存池 XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.tg = self.tgs[indexPath.row]; return cell; }