一、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. P3456 [POI2007]GRZ-Ridges and Valleys(bfs)

    P3456 [POI2007]GRZ-Ridges and Valleys 八个方向都跑一遍bfs,顺便判断一下是山峰还是山谷,或者是山坡(俩都不是) (实在不知道要说啥了qwq) #include& ...

  2. 01: 企业微信API开发前准备

    目录:企业微信API其他篇 01: 企业微信API开发前准备 02:消息推送 03: 通讯录管理 04:应用管理 目录: 1.1 术语介绍 1.2 开发步骤 1.1 术语介绍返回顶部 参考文档:htt ...

  3. js实现ajax请求

    练下手,好久没写ajax var xmlhttp=null;//创建XMLHttprequest function createXMLHttpRequest(){ if(window.ActiveXO ...

  4. shell:遍历目录和子目录的所有文件及匹配文件内容到日志

    过滤文件内网 #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] ...

  5. redis安装使用配置

    一.安装前的准备 下载redis http://redis.io/download https://github.com/mythz/redis-windows 下载Windows版客户端net版sd ...

  6. zedgraph右键菜单的汉化

    http://blog.csdn.net/jeryler/article/details/7876376 修改 zedGraphControl的ContextMenuBuilder事件即可! zedG ...

  7. 【第九章】 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  8. Java 多线程 破解密码 demo

    功能要求: 具体类: Decrypt  测试类,用来启动破解和日志线程 DecryptThread 破解线程类,用来生成测试的字符串,并暴力破解 LogThread 日志类,将输出每次生成的字符串结果 ...

  9. Chromosome coordinate systems: 0-based, 1-based

    From: https://arnaudceol.wordpress.com/2014/09/18/chromosome-coordinate-systems-0-based-1-based/ I’v ...

  10. CodeForces 1029E div3

    题目链接 第一道场上自己做出来的E题...虽然是div3,而且是原题... 当时做完ABC,D题没有思路就去怼E了,然后发现貌似原题? 事实上就是原题... 给个原题链接... [HNOI2003]消 ...