UITableView定義等高的cell

1 純代碼方式

新建一個繼承自UITableViewCell的子類,好比XMGTgCell

@interface XMGTgCell : UITableViewCell
@end

在XMGTgCell.m文件中

  • 重寫-initWithStyle:reuseIdentifier:方法
    • 在這個方法中添加全部須要顯示的子控件
    • 給子控件作一些初始化設置(設置字體、文字顏色等)
    • (或者使用masonry框架,在這個方法中設置位置,而不須要在layoutSubviews函數中設置位置了,這種叫作autolayout方式)。
/**
 *  在這個方法中添加全部的子控件
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // ......
    }
    return self;
}
  • 重寫-layoutSubviews方法
    • 必定要調用[super layoutSubviews]
    • 在這個方法中計算和設置全部子控件的frame
/**
 *  在這個方法中計算全部子控件的frame
 */
- (void)layoutSubviews
{
    [super layoutSubviews];

    // ......
}

在XMGTgCell.h文件中提供一個模型屬性,好比XMGTg模型

@class XMGTg;

@interface XMGTgCell : UITableViewCell
/** 團購模型數據 */
@property (nonatomic, strong) XMGTg *tg;
@end

在XMGTgCell.m中重寫模型屬性的set方法

  • 在set方法中給子控件設置模型數據
- (void)setTg:(XMGTg *)tg
{
    _tg = tg;

    // .......
}

定義模型block

定義模型的時候,能夠將模型所作的事情也同時定義在模型中,用一個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];

在控制器中

  • 註冊cell的類型
[self.tableView registerClass:[XMGTgCell class] forCellReuseIdentifier:ID];
  • 給cell傳遞模型數據
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 訪問緩存池
    XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 設置數據(傳遞模型數據)
    cell.tg = self.tgs[indexPath.row];

    return cell;
}

2 xib方式

新建一個繼承自UITableViewCell的子類,好比XMGTgCell

@interface XMGTgCell : UITableViewCell
@end

新建一個xib文件(文件名最好跟類名一致,好比XMGTgCell.xib)

  • 修改cell的class爲XMGTgCell

  • 綁定循環利用標識

  • 添加子控件,設置子控件約束

  • 將子控件連線到類擴展中
@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

在XMGTgCell.h文件中提供一個模型屬性,好比XMGTg模型

@class XMGTg;

@interface XMGTgCell : UITableViewCell
/** 團購模型數據 */
@property (nonatomic, strong) XMGTg *tg;
@end

在XMGTgCell.m中重寫模型屬性的set方法

  • 在set方法中給子控件設置模型數據
- (void)setTg:(XMGTg *)tg
{
    _tg = tg;

    // .......
}

在控制器中

  • 註冊xib文件
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
  • 給cell傳遞模型數據
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 訪問緩存池
    XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 設置數據(傳遞模型數據)
    cell.tg = self.tgs[indexPath.row];

    return cell;
}

3 storyBoard方式

新建一個繼承自UITableViewCell的子類,好比XMGTgCell

@interface XMGTgCell : UITableViewCell
@end

在storyboard文件中,找到UITableView裏面的cell(動態cell)

  • 修改cell的class爲XMGTgCell

  • 綁定循環利用標識

  • 添加子控件,設置子控件約束

  • 將子控件連線到類擴展中
@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

在XMGTgCell.h文件中提供一個模型屬性,好比XMGTg模型

@class XMGTg;

@interface XMGTgCell : UITableViewCell
/** 團購模型數據 */
@property (nonatomic, strong) XMGTg *tg;
@end

在XMGTgCell.m中重寫模型屬性的set方法

  • 在set方法中給子控件設置模型數據
- (void)setTg:(XMGTg *)tg
{
    _tg = tg;

    // .......
}

在控制器中

  • 給cell傳遞模型數據
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"tg";
    // 訪問緩存池
    XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 設置數據(傳遞模型數據)
    cell.tg = self.tgs[indexPath.row];

    return cell;
}
相關文章
相關標籤/搜索