自定义等高的cell(代码创建frame)
来源:互联网 发布:sqlserver history 编辑:程序博客网 时间:2024/05/16 15:07
//// tgCell.h#import <UIKit/UIKit.h>@class tgModel;@interface tgCell : UITableViewCell/** * 团购模型数据 */@property(nonatomic,strong)tgModel *model;/** * 创建一个cell */+ (instancetype)cellWithTableView:(UITableView *)tableView;@end
//// tgCell.m#import "tgCell.h"#import "tgModel.h"@interface tgCell()@property (weak, nonatomic) UIImageView *iconView;@property (weak, nonatomic) UILabel *titleLabel;@property (weak, nonatomic) UILabel *priceLabel;@property (weak, nonatomic) UILabel *buyCountLabel;@end@implementation tgCell// 1.在initWithStyle:reuseIdentifier:方法中添加子控件- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { UIImageView *iconView = [[UIImageView alloc] init]; [self.contentView addSubview:iconView]; self.iconView = iconView; UILabel *titleLabel = [[UILabel alloc] init]; [self.contentView addSubview:titleLabel]; self.titleLabel = titleLabel; UILabel *priceLabel = [[UILabel alloc] init]; priceLabel.textColor = [UIColor orangeColor]; [self.contentView addSubview:priceLabel]; self.priceLabel = priceLabel; UILabel *buyCountLabel = [[UILabel alloc] init]; buyCountLabel.textAlignment = NSTextAlignmentRight; buyCountLabel.font = [UIFont systemFontOfSize:14]; buyCountLabel.textColor = [UIColor lightGrayColor]; [self.contentView addSubview:buyCountLabel]; self.buyCountLabel = buyCountLabel; } return self;}// 2.在layoutSubviews方法中设置子控件的frame- (void)layoutSubviews{ [super layoutSubviews]; CGFloat contentH = self.contentView.frame.size.height; CGFloat contentW = self.contentView.frame.size.width; CGFloat margin = 10; CGFloat iconX = margin; CGFloat iconY = margin; CGFloat iconW = 100; CGFloat iconH = contentH - 2 * iconY; self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH); // titleLabel CGFloat titleX = CGRectGetMaxX(self.iconView.frame) + margin; CGFloat titleY = iconY; CGFloat titleW = contentW - titleX - margin; CGFloat titleH = 21; self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH); // CGRectMake(titleX, titleY, titleW, titleH); // priceLabel CGFloat priceX = titleX; CGFloat priceH = 21; CGFloat priceY = contentH - margin - priceH; CGFloat priceW = 70; self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH); // buyCountLabel CGFloat buyCountH = priceH; CGFloat buyCountY = priceY; CGFloat buyCountX = CGRectGetMaxX(self.priceLabel.frame) + margin; CGFloat buyCountW = contentW - buyCountX - margin; self.buyCountLabel.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);}// 3.重写模型的set方法- (void)setModel:(tgModel *)model{ _model = model; //设置数据 self.iconView.image = [UIImage imageNamed:model.icon]; self.titleLabel.text = model.title; self.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.price]; self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买",model.buyCount];}+ (instancetype)cellWithTableView:(UITableView *)tableView{ static NSString *ID = @"cell"; tgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[tgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell;}@end
0 0
- 自定义等高的cell(代码创建frame)
- UI 一一 自定义等高cell (纯代码-Frame)方式
- 自定义等高的cell(纯代码)
- 自定义等高的cell(代码Autolayout)
- 自定义不等高的cell(纯代码frame)
- 自定义不等高cell的纯代码步骤(frame)
- 自定义等高的cell
- 自定义等高的cell
- tableView - 自定义等高cell(frame和masonry方式)
- 自定义cell的frame
- cell的自定义frame
- 自定义等高的cell(使用stroyboard)
- 自定义等高的cell(使用XIB)
- 有关自定义等高的cell方法
- 自定义等高的cell(storyboard)
- 自定义等高的cell(xib)
- xib自定义非等高的cell
- storyboard自定义非等高的Cell
- HDOJ 5606-tree【并查集】
- 【转载】光流法简单介绍
- 运行MIT-GNU Scheme出现“Requested allocation is too large”
- 总结一下自己的2015
- leetcode学习笔记5
- 自定义等高的cell(代码创建frame)
- Fill the Square
- 【Java设计模式】详解系列 3.1.2行为型模式 : 观察者模式 — 模式详解
- shell 中的continue
- HDU 1005.Number Sequence【用递归会超时】(2.5)
- eval解析JSON注意点
- Theano 逻辑回归教程 的注释
- HDU 1247 Hat’s Words
- HTML5开发移动web应用——Sencha Touch篇(8)