一、Model

BWWeiBo数据模型

 #import <Foundation/Foundation.h>

 @interface BWWeiBo : NSObject

 @property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *picture;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign, getter=isVip) BOOL vip; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)weiBoWithDict:(NSDictionary *)dict; @end #import "BWWeiBo.h" @implementation BWWeiBo - (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)weiBoWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end

BWWeiBoFrame的控件尺寸模型

 #import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>
@class BWWeiBo; @interface BWWeiBoFrame : NSObject @property (nonatomic, strong) BWWeiBo *weibo; @property (nonatomic, assign) CGRect iconFrame;
@property (nonatomic, assign) CGRect nameFrame;
@property (nonatomic, assign) CGRect vipFrame;
@property (nonatomic, assign) CGRect textFrame;
@property (nonatomic, assign) CGRect picFrame; @property (nonatomic, assign) CGFloat rowHeight; @end #import "BWWeiBoFrame.h"
#import "BWWeiBo.h" #define nameFont [UIFont systemFontOfSize:12]
#define textFont [UIFont systemFontOfSize:14] @implementation BWWeiBoFrame //重写weibo的set方法同时设置控件的frame
- (void)setWeibo:(BWWeiBo *)weibo
{
_weibo = weibo;
//提取统一的间距
CGFloat margin = ;
//1.头像
CGFloat iconW = ;
CGFloat iconH = ;
CGFloat iconX = margin;
CGFloat iconY = margin;
_iconFrame = CGRectMake(iconX, iconY, iconW, iconH); //2.昵称
// 获取昵称字符串
NSString *nickName = weibo.name;
// 根据Label中文字的内容,动态计算Label的高和宽
CGSize nameSize = [self sizeWithText:nickName andMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) andFont:nameFont]; CGFloat nameX = CGRectGetMaxX(_iconFrame) +margin;
CGFloat nameW = nameSize.width;
CGFloat nameH = nameSize.height;
CGFloat nameY = iconY + (iconH - nameH)/;
_nameFrame = CGRectMake(nameX, nameY, nameW, nameH); //3.会员
CGFloat vipW = ;
CGFloat vipH = ;
CGFloat vipX = CGRectGetMaxX(_nameFrame) + margin;
CGFloat vipY = nameY;
_vipFrame = CGRectMake(vipX, vipY, vipW, vipH); //4.正文
CGFloat textX = iconX;
CGFloat textY = CGRectGetMaxY(_iconFrame) +margin;
CGSize textSize = [self sizeWithText:weibo.text andMaxSize:CGSizeMake(, MAXFLOAT) andFont:textFont];
CGFloat textW = textSize.width;
CGFloat textH = textSize.height;
_textFrame = CGRectMake(textX, textY, textW, textH); //5.配图
CGFloat picW = ;
CGFloat picH = ;
CGFloat picX = iconX;
CGFloat picY = CGRectGetMaxY(_textFrame) + margin;
_picFrame = CGRectMake(picX, picY, picW, picH); //6.计算设置每行行高
if (self.weibo.picture) {
self.rowHeight = CGRectGetMaxY(_picFrame) + margin;
}
else
{
self.rowHeight = CGRectGetMaxY(_textFrame) +margin;
} } //根据给定的字符串、最大值size、给定的字体、来计算文字应该占用的大小 /**
* 计算文字的尺寸
*
* @param text 所要计算的文字
* @param maxSize 规定的文字尺寸范围,一般直限制宽度,而不限制高度
* @param font 计算文字时所用的字体"计算时用的字体大小,要和显示时的字体一样"
*
* @return 计算出来的文字尺寸
*/
- (CGSize) sizeWithText:(NSString *)text andMaxSize:(CGSize)maxSize andFont:(UIFont *)font
{
NSDictionary *attr = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
} @end

二、View

 #import <UIKit/UIKit.h>
@class BWWeiBoFrame; @interface BWWeiBoCell : UITableViewCell @property (nonatomic, strong) BWWeiBoFrame *weiboFrame; + (instancetype)weiboCellWithTableView:(UITableView *)tableView; @end #import "BWWeiBoCell.h"
#import "BWWeiBo.h"
#import "BWWeiBoFrame.h"
#define nameFont [UIFont systemFontOfSize:12]
#define textFont [UIFont systemFontOfSize:14] @interface BWWeiBoCell () @property (nonatomic, strong) UIImageView *imgViewIcon;
@property (nonatomic, strong) UILabel *lblNickName;
@property (nonatomic, strong) UIImageView *imgViewVip;
@property (nonatomic, strong) UILabel *lblText;
@property (nonatomic, strong) UIImageView *imgViewPicture; @end @implementation BWWeiBoCell #pragma mark - 重写单元格的initWithStyle:方法
// 重写initWithStyle:方法在此方法中来创建自定义cell中所有要显示内容的子控件,在此方法中只创建和添加所有的子控件,并对子控件做一次的设置,不用设置子控件的数据和frame,因为此方法只会调用几次,当缓存池中有可重用的cell时,就不会调用此方法来创建cell了
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//创建控件 //1.头像
self.imgViewIcon = [[UIImageView alloc] init];
[self.contentView addSubview:self.imgViewIcon];
//2.昵称
self.lblNickName = [[UILabel alloc] init];
[self.contentView addSubview:self.lblNickName];
//3.会员
self.imgViewVip = [[UIImageView alloc] init];
[self.contentView addSubview:self.imgViewVip];
//4.正文
self.lblText = [[UILabel alloc] init];
[self.contentView addSubview:self.lblText];
//5.配图
self.imgViewPicture = [[UIImageView alloc] init];
[self.contentView addSubview:self.imgViewPicture];
}
return self;
}
//创建单元格
+ (instancetype)weiboCellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"weiBo_cell"; BWWeiBoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[BWWeiBoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
}
#pragma mark - 设置数据和frame
- (void)setWeiboFrame:(BWWeiBoFrame *)weiboFrame
{
_weiboFrame = weiboFrame;
//1.设置当前单元格中子控件的数据
[self settingData];
//2.设置当前单元格中子控件的frame
[self settingFrame];
} //设置数据的方法
- (void)settingData
{
BWWeiBo *weibo = self.weiboFrame.weibo;
//1.头像
self.imgViewIcon.image = [UIImage imageNamed:weibo.icon];
//2.昵称
self.lblNickName.text = weibo.name;
self.lblNickName.font = nameFont;
//3.会员
if (weibo.isVip) {
self.imgViewVip.image = [UIImage imageNamed:@"vip.png"];
//显示会员图标
self.imgViewVip.hidden = NO;
//设置昵称的颜色
self.lblNickName.textColor = [UIColor redColor];
}
else{
//隐藏会员图标
self.imgViewVip.hidden = YES;
//设置昵称的颜色
self.lblNickName.textColor = [UIColor blackColor];
}
//4.正文
self.lblText.text = weibo.text;
self.lblText.font = textFont;
self.lblText.numberOfLines = ;
//5.配图
if (weibo.picture) {
self.imgViewPicture.image = [UIImage imageNamed:weibo.picture];
self.imgViewPicture.hidden = NO;
}
else{
self.imgViewPicture.hidden = YES;
}
} //设置Frame
- (void)settingFrame
{
//1.头像
self.imgViewIcon.frame = self.weiboFrame.iconFrame; //2.昵称
self.lblNickName.frame = self.weiboFrame.nameFrame; //3.会员 self.imgViewVip.frame = self.weiboFrame.vipFrame; //4.正文
self.lblText.frame = self.weiboFrame.textFrame; //5.配图
self.imgViewPicture.frame = self.weiboFrame.picFrame; } - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end

三、Controller

 #import "BWTableViewController.h"
#import "BWWeiBo.h"
#import "BWWeiBoFrame.h"
#import "BWWeiBoCell.h" @interface BWTableViewController () //现在要求weiboFrame集合中保存了很多个BWWeiBoFrame模型,不再是BWWeiBo模型
@property (nonatomic, strong)NSArray *weiboFrames; @end @implementation BWTableViewController #pragma mark - 懒加载
- (NSArray *)weiboFrames
{
if (!_weiboFrames) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrModel = [NSMutableArray array]; for (NSDictionary *dict in arrDict) {
//创建数据模型
BWWeiBo *modelData = [BWWeiBo weiBoWithDict:dict]; BWWeiBoFrame *modelFrame = [[BWWeiBoFrame alloc] init]; modelFrame.weibo = modelData; [arrModel addObject:modelFrame];
}
_weiboFrames = arrModel;
}
return _weiboFrames;
} - (void)viewDidLoad {
[super viewDidLoad];
//self.tableView.rowHeight = 400; // Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.weiboFrames count]; } //创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1.获取数据模型
BWWeiBoFrame *model = [self.weiboFrames objectAtIndex:indexPath.row]; //2.创建单元格
BWWeiBoCell *cell = [BWWeiBoCell weiboCellWithTableView:tableView]; //3.设置单元格数据
cell.weiboFrame = model; //4.返回单元格
return cell;
} #pragma mark - UITableViewDelegate
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BWWeiBoFrame *weiboFrame = self.weiboFrames[indexPath.row];
return weiboFrame.rowHeight; } - (BOOL)prefersStatusBarHidden
{
return YES;
}
/*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
05-11 08:28