纯代码自定义cell

自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样)

  • 1.新建一个继承自UITableViewCell的子类

  • 2.在initWithStyle:方法中进行子控件的初始化

    • 2.1将有可能显示的所有子控件添加到contentView中,代码如下
    • 2.2顺便设置子控件的一些属性(一次性属性:文字颜色,字体,背景)
/// cell初始化方法中对子控件进行初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
/** cell顶部的容器 */
UIView *topContainerView = [[UIView alloc] init];
[self.contentView addSubview:topContainerView];
self.topContainerView = topContainerView;
/** 头像图片 */
UIImageView *headerImageView = [[UIImageView alloc] init];
[self.topContainerView addSubview:headerImageView];
self.headerImageView = headerImageView;
/** 会员图片 */
UIImageView *vipImageView = [[UIImageView alloc] init];
[self.topContainerView addSubview:vipImageView];
self.vipImageView = vipImageView;
/** 微博图片 */
UIImageView *photoImageView = [[UIImageView alloc] init];
[self.topContainerView addSubview:photoImageView];
self.photoImageView = photoImageView;
/** 名称 */
UILabel *nameLabel = [[UILabel alloc] init];
[self.topContainerView addSubview:nameLabel];
self.nameLabel = nameLabel;
/** 时间 */
UILabel *timeLabel = [[UILabel alloc] init];
[self.topContainerView addSubview:timeLabel];
self.timeLabel = timeLabel;
/** 来源 */
UILabel *sourceLabel = [[UILabel alloc] init];
[self.topContainerView addSubview:sourceLabel];
self.sourceLabel = sourceLabel;
/** 正文 */
UILabel *contentLabel = [[UILabel alloc] init];
[self.topContainerView addSubview:contentLabel];
self.contentLabel = contentLabel;
}
return self;
}
  • 3.提供两个模型

    • 3.1一个是数据模型(文字数据 + 图片数据)
    • 3.2一个是frame模型(数据模型 + 所有子控件的frame + cell的高度)

/// frame模型中重写数据模型的set方法,根据数据计算各控件的frame
- (void)setStatus:(ChaosStatus *)status
{
_status = status;
ChaosUser *user = status.user;
// 计算frame /** 头像图片 */
CGFloat headerX = ChaosStatusCellBorderWidth;
CGFloat headerY = ChaosStatusCellBorderWidth;
CGFloat headerWH = 50;
self.headerImageViewF = CGRectMake(headerX, headerY, headerWH, headerWH); /** 名称 */
CGFloat nameLabelX = CGRectGetMaxX(self.headerImageViewF) + ChaosStatusCellBorderWidth;
CGFloat nameLabelY = headerY; CGSize nameLabelSize = [self sizeWithText:user.screen_name font:ChaosStatusNameLabelFont];
self.nameLabelF = (CGRect){{nameLabelX,nameLabelY},nameLabelSize}; /** 会员图片 */
CGFloat vipX = CGRectGetMaxX(self.nameLabelF) + ChaosStatusCellBorderWidth;
CGFloat vipY = headerY;
CGFloat vipW = 25;
CGFloat vipH = nameLabelSize.height;
if (status.user.isVip) {
self.vipImageViewF = CGRectMake(vipX, vipY, vipW, vipH);
} /** 时间 */
CGFloat timeLabelX = nameLabelX;
CGFloat timeLabelY = CGRectGetMaxY(self.nameLabelF) + ChaosStatusCellBorderWidth;
CGSize timeLabelSize = [self sizeWithText:status.created_at font:ChaosStatusTimeLabelFont];
self.timeLabelF = (CGRect){{timeLabelX,timeLabelY},timeLabelSize}; /** 来源 */
CGFloat sourceLabelX = CGRectGetMaxX(self.timeLabelF) + ChaosStatusCellBorderWidth;
CGFloat sourceLabelY = timeLabelY;
CGSize sourceLabelSize = [self sizeWithText:status.source font:ChaosStatusSourceLabelFont];
self.sourceLabelF = (CGRect){{sourceLabelX,sourceLabelY},sourceLabelSize}; /** 正文 */
CGFloat contentX = headerX;
CGFloat contentY = MAX(CGRectGetMaxY(self.headerImageViewF), CGRectGetMaxY(self.timeLabelF)) + 10;
CGSize contentSize = [self sizeWithText:status.text font:ChaosStatusContentLabelFont maxWidth:ChaosScreenSize.width - 2 * ChaosStatusCellBorderWidth];
self.contentLabelF = (CGRect){{contentX,contentY},contentSize};
/** 微博图片 */ /** cell顶部的容器 */
self.topContainerViewF = CGRectMake(0, 0, ChaosScreenSize.width, CGRectGetMaxY(self.contentLabelF)); /** cell高度 */
self.cellHeight = CGRectGetMaxY(self.topContainerViewF) + ChaosStatusCellBorderWidth;
}
  • 4.cell应该提供一个frame模型属性

    • 4.1将frame模型传递给cell
    • 4.2cell根据frame模型给子控件设置frame,根据数据模型给子控件设置数据
    • 4.3cell根据数据模型决定显示和隐藏那些子控件
/// cell中重写模型的set,方法中设置控件的frame.设置控件的数据
- (void)setStatusFrame:(ChaosStatusFrame *)statusFrame
{
_statusFrame = statusFrame; ChaosStatus *status = statusFrame.status;
ChaosUser *user = status.user; /** 头像图片 */
self.headerImageView.frame = statusFrame.headerImageViewF;
[self.headerImageView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default_small"]]; /** 会员图片 */
self.vipImageView.frame = statusFrame.vipImageViewF;
self.vipImageView.contentMode = UIViewContentModeCenter;
if (user.isVip) { // 是会员
self.vipImageView.hidden = NO;
NSString *str = [NSString stringWithFormat:@"common_icon_membership_level%d",user.mbrank];
self.vipImageView.image = [UIImage imageNamed:str];
// 文字颜色
self.nameLabel.textColor = [UIColor orangeColor];
} else { // 不是会员
self.vipImageView.hidden = YES;
// 文字颜色
self.nameLabel.textColor = [UIColor blackColor];
} /** 名称 */
self.nameLabel.frame = statusFrame.nameLabelF;
self.nameLabel.font = ChaosStatusNameLabelFont;
self.nameLabel.text = user.screen_name; /** 时间 */
self.timeLabel.frame = statusFrame.timeLabelF;
self.timeLabel.font = ChaosStatusTimeLabelFont;
self.timeLabel.text = status.created_at;
self.timeLabel.textColor = [UIColor grayColor]; /** 来源 */
self.sourceLabel.frame = statusFrame.sourceLabelF;
self.sourceLabel.font = ChaosStatusSourceLabelFont;
self.sourceLabel.text = status.source;
self.sourceLabel.textColor = [UIColor lightGrayColor]; /** 正文 */
self.contentLabel.frame = statusFrame.contentLabelF;
self.contentLabel.text = status.text;
self.contentLabel.numberOfLines = 0;
self.contentLabel.textColor = [UIColor blackColor];
self.contentLabel.font = ChaosStatusContentLabelFont; /** 微博图片 */
self.photoImageView.frame = statusFrame.photoImageViewF; /** cell顶部的容器 */
self.topContainerView.frame = statusFrame.topContainerViewF;
self.topContainerView.backgroundColor = [UIColor orangeColor];
}
  • 5在tableView的代理方法中返回cell的高度

iOS开发小技巧--纯代码自定义cell的更多相关文章

  1. iOS开发小技巧--iOS8之后的cell自动计算高度

    cell高度自动计算步骤:

  2. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  3. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  4. 李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView

    李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView (VTmagic是一个实现左右滚动的控制器的框架,也可以实现此功能) 实现的效果:  01 - 创建四个控制器 02 - 定义需要 ...

  5. AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI

    AJ分享,必须精品 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用get ...

  6. iOS开发小技巧--设置cell左右有空隙,设置分割线的新思路,重写setFrame:让别人在外界无法修改控件的大小

    如图:需要自定义cell

  7. iOS开发小技巧 -- tableView-section圆角边框解决方案

    [iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...

  8. iOS开发——OC篇&纯代码退出键盘

    关于iOS开发中键盘的退出,其实方法有很多中,而且笔者也也学会了不少,包括各种非纯代码界面的退出. 但是最近开始着手项目的时候却闷了,因为太多了,笔者确实知道有很多中方法能实现,而且令我影响最深的就是 ...

  9. iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧

    1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...

随机推荐

  1. MVC、MVVM、MVP小结

    MVC MVC(Mode View Controller)是一种设计模式,它将应用划分为三个部分: 数据(模型).展现层(视图).用户交互(控制器). 一个事件发生的过程: ① 用户和应用产生交互 ② ...

  2. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

  3. JAVA 字符串驻留池

    一切从String str = new String("abc")说起...    这行代码形式上很简单,其实很复杂.有一个常见的Java笔试题就是问上面这行代码创建了几个Stri ...

  4. JavaScript语言精粹笔记

    JavaScript语言精粹笔记 掌握语言的每个特性可以让你出风头,但是并不推荐,因为一部分的特性带来的麻烦可能远超本身的价值.正如书中所言,坏的材料并不能雕刻出好的作品,要成为一名更好的程序员,要取 ...

  5. ThreadLocal用法和实现原理

    如果你定义了一个单实例的java bean,它有若干属性,但是有一个属性不是线程安全的,比如说HashMap.并且碰巧你并不需要在不同的线程中共享这个属性,也就是说这个属性不存在跨线程的意义.那么你不 ...

  6. jQuery语法

    目录: 一.选择网页元素二.改变结果集三.链式操作四.元素的操作五.元素的操作:移动六.元素的操作:复制.删除和创建七.工具方法八.事件操作九.特殊效果 一.选择网页元素这也是jQuery的基本设计思 ...

  7. 我心目中的Asp.net核心对象

    转:http://www.cnblogs.com/fish-li/archive/2011/08/21/2148640.html 阅读目录 开始 HttpRuntime HttpServerUtili ...

  8. Screen 对象

    Screen 对象 Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 注意: 没有应用于 screen 对象的公开标准,不过所有浏览器都支持该对象. Screen 对象属性 属性 ...

  9. PAT 1031. 查验身份证(15)

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...

  10. vijos P1448 校门外的树

    描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的--如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:\(K=1\),读入\(l, ...