tableView - 自定义等高cell(frame和masonry方式)
来源:互联网 发布:入骨相思知不知 编辑:程序博客网 时间:2024/06/04 16:35
代码自定义cell方法
代码自定义cell(使用frame)
- 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
- 在initWithStyle:reuseIdentifier:方法中
- 添加子控件
- 设置子控件的初始化属性(比如文字颜色、字体)
- 在layoutSubviews方法中设置子控件的frame
- 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
- 在initWithStyle:reuseIdentifier:方法中
- 2.在控制器中
- 利用registerClass…方法注册WQDealCell类
- 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
- 给cell传递模型数据
- 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
- 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
#import <UIKit/UIKit.h>@class WQdealsData;@interface WQTableViewCell : UITableViewCell/**数据模型接口*/@property (nonatomic, strong) WQdealsData *dealData;+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView;@end#import "WQTableViewCell.h"#import "WQdealsData.h"@interface WQTableViewCell ()/**图片*/@property (nonatomic, strong) UIImageView *iconImageView;/**标题*/@property (nonatomic, strong) UILabel *titleLabel;/**价格*/@property (nonatomic, strong) UILabel *priceLabel;/**购买人数*/@property (nonatomic, strong) UILabel *buyCountLabel;@end@implementation WQTableViewCell+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView{ static NSString *ID = @"deals"; WQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 当使用注册方法优化cell创建的时候,cell永远不为nil,不进入该方法 if (cell == nil) { cell = [[WQTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell;}// 1.在initWithStyle:reuseIdentifier:方法中添加子控件,背景色用于测试添加位置是否正确- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // 添加imageView self.iconImageView = [[UIImageView alloc]init]; _iconImageView.backgroundColor = [UIColor redColor]; [self.contentView addSubview:self.iconImageView]; // 添加title self.titleLabel = [[UILabel alloc]init]; //self.titleLabel.backgroundColor = [UIColor blueColor]; //self.titleLabel.textColor = [UIColor redColor]; [self.contentView addSubview:self.titleLabel]; // 添加price self.priceLabel = [[UILabel alloc]init]; //self.priceLabel.backgroundColor = [UIColor orangeColor]; self.priceLabel.textColor = [UIColor redColor]; [self.contentView addSubview:self.priceLabel]; // 添加buyCount self.buyCountLabel = [[UILabel alloc]init]; //self.buyCountLabel.backgroundColor = [UIColor grayColor]; self.buyCountLabel.textColor = [UIColor redColor]; self.buyCountLabel.textAlignment = NSTextAlignmentRight; [self.contentView addSubview:self.buyCountLabel]; } return self;}// 3,在数据模型接口setter方法中,给控件赋值- (void)setDealData:(WQdealsData *)dealData{ _dealData = dealData; self.iconImageView.image = [UIImage imageNamed:_dealData.icon]; self.titleLabel.text = _dealData.title; self.priceLabel.text = [NSString stringWithFormat:@"%@¥", _dealData.price]; self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", _dealData.buyCount];}// 2.在layoutSubviews方法中设置子控件的frame- (void)layoutSubviews{ [super layoutSubviews]; CGFloat contentW = self.contentView.frame.size.width; CGFloat contentH = self.contentView.frame.size.height; CGFloat margin = 10; // imageView CGFloat iconX = margin; CGFloat iconY = margin; CGFloat iconW = 100; CGFloat iconH = contentH - margin * 2; self.iconImageView.frame = CGRectMake(iconX, iconY, iconW, iconH); // title CGFloat titleX = CGRectGetMaxX(self.iconImageView.frame) + margin; CGFloat titleY = margin; CGFloat titleW = contentW - titleX - margin; CGFloat titleH = 20; self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH); // price CGFloat priceX = titleX; CGFloat priceY = margin * 2 + titleH; CGFloat priceW = 80; CGFloat priceH = contentH - margin * 3 - titleH; self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH); // buyCount CGFloat buyCountX = CGRectGetMaxX(self.priceLabel.frame); CGFloat buyCountY = priceY; CGFloat buyCountW = contentW - buyCountX - margin; CGFloat buyCountH = priceH; self.buyCountLabel.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);}// "WQTableViewController.h" 控制器中创建cell,控制器不需要知道cell具体实现,只创建传入数据即可
代码自定义cell(使用autolayout)
- 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
- 在initWithStyle:reuseIdentifier:方法中
- 添加子控件
- 添加子控件的约束(建议使用
Masonry
) - 设置子控件的初始化属性(比如文字颜色、字体)
- 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
- 在initWithStyle:reuseIdentifier:方法中
- 2.在控制器中
- 利用registerClass…方法注册WQDealCell类
- 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
- 给cell传递模型数据
- 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
- 1.创建一个继承自UITableViewCell的子类,比如WQDealCell
//两个宏定义一定要在头文件之前,因为masonry实现文件里需要用到//define this constant if you want to use Masonry without the 'mas_' prefix#define MAS_SHORTHAND//define this constant if you want to enable auto-boxing for default syntax#define MAS_SHORTHAND_GLOBALS#import "Masonry.h"//创建完子控件后,直接添加约束// 1.在initWithStyle:reuseIdentifier:方法中添加子控件,背景色用于测试添加位置是否正确- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { CGFloat margin = 10; // 添加imageView self.iconImageView = [[UIImageView alloc]init]; [self.contentView addSubview:self.iconImageView]; // 添加约束 [self.iconImageView makeConstraints:^(MASConstraintMaker *make) { make.top.left.offset(margin); make.bottom.offset(- margin); make.width.equalTo(100); }]; // 添加title self.titleLabel = [[UILabel alloc]init]; [self.contentView addSubview:self.titleLabel]; // 添加约束 [self.titleLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.iconImageView.right).offset(margin); make.top.offset(margin); make.right.offset(- margin); }]; // 添加price self.priceLabel = [[UILabel alloc]init]; self.priceLabel.textColor = [UIColor redColor]; [self.contentView addSubview:self.priceLabel]; // 添加约束 [self.priceLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.titleLabel.left); make.width.equalTo(80); make.bottom.offset(-margin); }]; // 添加buyCount self.buyCountLabel = [[UILabel alloc]init]; self.buyCountLabel.textColor = [UIColor redColor]; self.buyCountLabel.textAlignment = NSTextAlignmentRight; [self.contentView addSubview:self.buyCountLabel]; // 添加约束 [self.buyCountLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.priceLabel.right).offset(margin); make.right.equalTo(- margin); make.bottom.equalTo(self.priceLabel.bottom); }]; } return self;}
0 0
- tableView - 自定义等高cell(frame和masonry方式)
- tableView - 自定义等高cell-storyBoard方式
- tableView - 自定义等高cell-xib方式
- UI 一一 自定义等高cell (纯代码-Frame)方式
- tableView-自定义非等高cell(1)
- tableView-自定义非等高cell(2)
- 自定义等高cell和不等高cell的方式对比
- 自定义等高的cell(代码创建frame)
- UI 一一 自定义不等高cell (Frame方式)
- tableview非等高的cell
- UI 一一 自定义等高cell (纯代码-Autolayout)方式
- UI 一一 自定义等高cell (XIB方式)
- UI 一一 自定义等高cell (storyboard方式)
- 自定义等高的cell
- iOS 自定义等高cell
- 自定义等高的cell
- 非等高cell,根据图片宽高,排布tableview
- 自定义等高的cell(纯代码)
- Swift-常量(let)和变量(var)
- hdu 5389 Zero Escape (dp)
- HDU 5387 Clock
- tableView - 自定义等高cell-xib方式
- 八种排序算法(C语言简易版)
- tableView - 自定义等高cell(frame和masonry方式)
- UVA 12486 Space Elevator(数位DP)
- HDU1466 计算直线的交点数
- KMP算法超详细入门
- arcgis切割多边形以及合并多边形
- 菜单状态更新
- android:layout_weight详解
- ArcGIS地质图矢量化技巧概要
- tableView-自定义非等高cell(1)