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; }