iOS开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
一、实现效果
二、使用纯代码自定义一个tableview的步骤
1.新建一个继承自UITableViewCell的类
2.重写initWithStyle:reuseIdentifier:方法
添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中)
进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)
3.提供2个模型
数据模型: 存放文字数据\图片数据
frame模型: 存放数据模型\所有子控件的frame\cell的高度
4.cell拥有一个frame模型(不要直接拥有数据模型)
5.重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame
6.frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)
三、文件结构和实现代码
1.文件结构
2.实现代码:
NJWeibo.h文件
NJWeibo.m文件
NJWeiboCell.h文件
NJWeiboCell.m文件
#import "NJWeiboCell.h"
#import "NJWeibo.h"#import "NJWeiboFrame.h"#define NJNameFont [UIFont systemFontOfSize:15]
#define NJTextFont [UIFont systemFontOfSize:16]@interface NJWeiboCell ()
/** * 头像 */@property (nonatomic, weak) UIImageView *iconView;/** * vip */@property (nonatomic, weak) UIImageView *vipView;/** * 配图 */@property (nonatomic, weak) UIImageView *pictureView;/** * 昵称 */@property (nonatomic, weak) UILabel *nameLabel;/** * 正文 */@property (nonatomic, weak) UILabel *introLabel;@end@implementation NJWeiboCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{ // NSLog(@"cellForRowAtIndexPath"); static NSString *identifier = @"status"; // 1.缓存中取 NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // 2.创建 if (cell == nil) { cell = [[NJWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } return cell;} /** * 构造方法(在初始化对象的时候会调用) * 一般在这个方法中添加需要显示的子控件 */- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 让自定义Cell和系统的cell一样, 一创建出来就拥有一些子控件提供给我们使用 // 1.创建头像 UIImageView *iconView = [[UIImageView alloc] init]; [self.contentView addSubview:iconView]; self.iconView = iconView; // 2.创建昵称 UILabel *nameLabel = [[UILabel alloc] init]; nameLabel.font = NJNameFont; // nameLabel.backgroundColor = [UIColor redColor]; [self.contentView addSubview:nameLabel]; self.nameLabel = nameLabel; // 3.创建vip UIImageView *vipView = [[UIImageView alloc] init]; vipView.image = [UIImage imageNamed:@"vip"]; [self.contentView addSubview:vipView]; self.vipView = vipView; // 4.创建正文 UILabel *introLabel = [[UILabel alloc] init]; introLabel.font = NJTextFont; introLabel.numberOfLines = 0; // introLabel.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:introLabel]; self.introLabel = introLabel; // 5.创建配图 UIImageView *pictureView = [[UIImageView alloc] init]; [self.contentView addSubview:pictureView]; self.pictureView = pictureView; } return self;} - (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame{ _weiboFrame = weiboFrame; // 1.给子控件赋值数据 [self settingData]; // 2.设置frame [self settingFrame];} /** * 设置子控件的数据 */- (void)settingData{ NJWeibo *weibo = self.weiboFrame.weibo; // 设置头像 self.iconView.image = [UIImage imageNamed:weibo.icon]; // 设置昵称 self.nameLabel.text = weibo.name; // 设置vip if (weibo.vip) { self.vipView.hidden = NO; self.nameLabel.textColor = [UIColor redColor]; }else { self.vipView.hidden = YES; self.nameLabel.textColor = [UIColor blackColor]; } // 设置内容 self.introLabel.text = weibo.text; // 设置配图 if (weibo.picture) {// 有配图 self.pictureView.image = [UIImage imageNamed:weibo.picture]; self.pictureView.hidden = NO; }else { self.pictureView.hidden = YES; }}/** * 设置子控件的frame */- (void)settingFrame{// 设置头像的frame
self.iconView.frame = self.weiboFrame.iconF; // 设置昵称的frame self.nameLabel.frame = self.weiboFrame.nameF; // 设置vip的frame self.vipView.frame = self.weiboFrame.vipF; // 设置正文的frame self.introLabel.frame = self.weiboFrame.introF; // 设置配图的frameif (self.weiboFrame.weibo.picture) {// 有配图
self.pictureView.frame = self.weiboFrame.pictrueF; }}/**
* 计算文本的宽高 * * @param str 需要计算的文本 * @param font 文本显示的字体 * @param maxSize 文本显示的范围 * * @return 文本占用的真实宽高 */- (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize{ NSDictionary *dict = @{NSFontAttributeName : font}; // 如果将来计算的文字的范围超出了指定的范围,返回的就是指定的范围 // 如果将来计算的文字的范围小于指定的范围, 返回的就是真实的范围 CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; return size;}@end
NJWeiboFrame.h文件
// 专门用来保存每一行数据的frame, 计算frame
#import <Foundation/Foundation.h>
@class NJWeibo;@interface NJWeiboFrame : NSObject/** * 头像的frame */@property (nonatomic, assign) CGRect iconF;/** * 昵称的frame */@property (nonatomic, assign) CGRect nameF;/** * vip的frame */@property (nonatomic, assign) CGRect vipF;/** * 正文的frame */@property (nonatomic, assign) CGRect introF;/** * 配图的frame */@property (nonatomic, assign) CGRect pictrueF;/** * 行高 */@property (nonatomic, assign) CGFloat cellHeight;/**
* 模型数据 */@property (nonatomic, strong) NJWeibo *weibo;@endNJWeiboFrame.m文件
主控制器
NJViewController.m文件
四、补充说明
由于系统提供的tableview可能并不能满足我们的开发需求,所以经常要求我们能够自定义tableview。
自定义tableview有两种方式,一种是使用xib创建,一种是使用纯代码的方式创建。
对于样式一样的tableview,通常使用xib进行创建,对于高度不一样,内容也不完全一致的通常使用纯代码进行自定义。