一、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

iOS UI-微博案例(通过代码自定义Cell)的更多相关文章

  1. iOS开发小技巧--纯代码自定义cell

    纯代码自定义cell 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样) 1.新建一个继承自UITableViewCell的子类 2.在initWithStyle:方 ...

  2. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  3. [iOS基础控件 - 6.7] 微博展示 使用代码自定义TableCell(动态尺寸)

    A.需求 1.类似于微博内容的展示 2.头像 3.名字 4.会员标志 5.内容 6.分割线 7.配图(可选,可有可无)   code source: https://github.com/hellov ...

  4. iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结

    iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结 项目中我们常见的自定义cell主要分为两种 等高cell:如应用列表.功能列表 非等高cell:如微博列表.QQ聊天页面 下面对这 ...

  5. 通过代码自定义cell 新浪微博页面显示

    通过代码自定义cell(cell的高度不一致)(如果高度一致的cell 用xib实现) 1.新建一个集成自UItableVIewCell的类 2.重写initWithStle :方法 - (insta ...

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

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

  7. oc学习之路----通过代码自定义cell

    需求背景:由于tableView中每一个cell的数据与布局都不一样,故不能用xib实现功能,这是用代码写自定义cell就有必要了. 步骤 1.新建一个继承自UITableViewCell的类 2.重 ...

  8. 通过代码自定义cell(cell的高度不一致,比如微博)

    1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 (先要调用父控件的nitWithStyle:reuseIdentifie ...

  9. IOS 通过 代码 自定义cell(Cell的高度不一致)(优化性能)

    创建cell的步骤 1.新建一个继承自UITabelViewCell的类 2.重写 initWithStyle:ReuseIdentifier: 方法 添加所有需要显示的子控件(不需要设置子控件的数据 ...

随机推荐

  1. centos 安装 ffmpeg

    使用yum方式安装ffmpeg: 先安装Nux Dextop仓库: Nux Dextop库依赖于EPEL库,所有要先安装EPEL库(需要管理员权限). 如果安装过则跳过. $ su root $ yu ...

  2. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  3. <offer4> 04_FindInPartiallySortedMatrix

    #include<cstdio> bool Find(int* matrix, int rows, int columns, int number) { bool result = fal ...

  4. 第八章 对称加密算法--AES

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 8.1.AES 特点: 密钥建立时间短.灵敏性好.内存需求低(不管怎样,反正就是好) ...

  5. Unity3D学习笔记(十八):动画内容补充

    动画系统: 旧动画系统(帧动画系统:关键帧驱动,关键帧记录的数据进行插值移动) 1.添加Animation,添加到父物体上 2.添加动画片段 3.添加关键帧(子物体的坐标是相对于父物体的坐标),帧之间 ...

  6. c#传统SqlTransaction事务和TransactionScope事务

    事务有很多种,看了一些关于事务的问题,这里做下笔记····· 事务时单个的工作单位.如果某一事务成功,则在该事务中进行的所有数据更改均会提交,成为数据库中永久的组成部分.若果事务遇到错误,则必须取消或 ...

  7. HDU 5889 Barricade(最短路+最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...

  8. HDU 5876 Sparse Graph(补图中求最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...

  9. UVa 10723 电子人的基因(LCS)

    https://vjudge.net/problem/UVA-10723 题意: 输入两个A~Z组成的字符串,找一个最短的串,使得输入的两个串均是它的子序列,另外还需要统计长度最短的串的个数. 思路: ...

  10. UVa 120 煎饼

    https://vjudge.net/problem/UVA-120 题意:颠倒连续子序列,使之成为升序. 思路:按照从大到小的顺序,依次选择出一个数字进行分析: ①如果该数字已经在正确的位置上,则不 ...