[iOS基础控件 - 6.7.1] 微博展示 代码

- //
- // ViewController.m
- //
- // Created by hellovoidworld on 14/12/4.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- #import "ViewController.h"
- #import "Weibo.h"
- #import "WeiboCell.h"
- #import "WeiboFrame.h"
- @interface ViewController ()
- /** 微博数组,类型是WeiboFrame,包含了数据和位置尺寸信息 */
- @property(nonatomic, strong) NSArray *weibos;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- // 屏蔽状态栏
- - (BOOL)prefersStatusBarHidden {
- return YES;
- }
- #pragma mark - 数据源操作
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.weibos.count;
- }
- - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- // 传入tableView是为了使用cell缓存池
- WeiboCell *cell = [WeiboCell cellWithTableView:self.tableView];
- // 传入微博的数据和位置尺寸信息
- cell.weiboFrame = self.weibos[indexPath.row];
- return cell;
- }
- #pragma mark - 加载数据
- // 延迟加载plist文件中的数据为微博数组
- - (NSArray *) weibos {
- if (nil == _weibos) {
- NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];
- NSMutableArray *mdictArray = [NSMutableArray array];
- for (NSDictionary *dict in dictArray) {
- WeiboFrame *weiboFrame = [[WeiboFrame alloc] init];
- Weibo *weibo = [Weibo weiboWithDictionary:dict];
- // 传入weibo模型数据到frame模型,内部保存数据,计算各个控件的位置、尺寸
- weiboFrame.weibo = weibo;
- [mdictArray addObject:weiboFrame];
- }
- _weibos = mdictArray;
- }
- return _weibos;
- }
- #pragma mark - 代理操作
- // 动态调整每个cell的高度
- - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- WeiboFrame *weiboFrame = self.weibos[indexPath.row];
- return weiboFrame.cellHeight;
- }
- @end
- //
- // WeiboCell.h
- //
- // Created by hellovoidworld on 14/12/5.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- // 用于装载每个TabelViewCell的model
- #import <UIKit/UIKit.h>
- @class WeiboFrame;
- @interface WeiboCell : UITableViewCell
- // 微博frame,内持有微博数据和尺寸、位置信息
- @property(nonatomic, strong) WeiboFrame *weiboFrame;
- // 自定义带有父控件tableView初始化方法
- + (instancetype) cellWithTableView:(UITableView *) tableView;
- @end
- //
- // WeiboCell.m
- //
- // Created by hellovoidworld on 14/12/5.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- #import "WeiboCell.h"
- #import "WeiboFrame.h"
- #import "Weibo.h"
- // 昵称字体
- #define NAME_FONT [UIFont systemFontOfSize:14]
- // 博文字体
- #define TEXT_FONT [UIFont systemFontOfSize:15]
- @interface WeiboCell()
- // 创建各个子控件的成员,用来分离数据赋值和尺寸、位置调整
- /** 头像 */
- @property(nonatomic, weak) UIImageView *iconView;
- /** 昵称 */
- @property(nonatomic, weak) UILabel *nameView;
- /** vip标志 */
- @property(nonatomic, weak) UIImageView *vipView;
- /** 博文 */
- @property(nonatomic, weak) UILabel *textView;
- /** 配图 */
- @property(nonatomic, weak) UIImageView *pictureView;
- @end
- @implementation WeiboCell
- - (void)awakeFromNib {
- // Initialization code
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- #pragma mark - 初始化
- // 自定义带有父控件tableView初始化方法
- + (instancetype) cellWithTableView:(UITableView *) tableView {
- static NSString *ID = @"weibo";
- // 从缓存池寻找
- WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
- // 使用重写的构造方法初始化
- if (nil == cell) {
- cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
- }
- return cell;
- }
- // 重写缓存池初始化方法,加入各个子控件,可以设置静态数据,但是没有动态的数据和位置尺寸信息
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
- // 1.头像
- /**
- 由于self.iconView是weak类型,不能写成:
- self.iconView = [[UIImageView alloc] init];
- 会被立即释放,不能正常赋值,下同
- */
- UIImageView *iconView = [[UIImageView alloc] init];
- [self.contentView addSubview:iconView];
- self.iconView = iconView;
- // 2.昵称
- UILabel *nameView = [[UILabel alloc] init];
- // 指定字体用来计算占用的尺寸大小
- nameView.font = NAME_FONT;
- [self.contentView addSubview:nameView];
- self.nameView = nameView;
- // 3.vip标志
- UIImageView *vipView = [[UIImageView alloc] init];
- vipView.image = [UIImage imageNamed:@"vip"];
- [self.contentView addSubview:vipView];
- self.vipView = vipView;
- // 4.博文
- UILabel *textView = [[UILabel alloc] init];
- textView.font = TEXT_FONT;
- textView.numberOfLines = ;// 设置自动换行
- [self.contentView addSubview:textView];
- self.textView = textView;
- // 5.配图
- UIImageView *pictureView = [[UIImageView alloc] init];
- [self.contentView addSubview:pictureView];
- self.pictureView = pictureView;
- }
- return self;
- }
- #pragma mark - 数据加载
- // 加载数据的时候设置数据和尺寸、位置
- - (void)setWeiboFrame:(WeiboFrame *)weiboFrame {
- _weiboFrame = weiboFrame;
- // 1.设置数据
- [self calWeiboData];
- // 2.设置尺寸、位置
- [self calWeiboFrame];
- }
- // 设置数据
- - (void) calWeiboData {
- Weibo *weibo = self.weiboFrame.weibo;
- // 1.头像
- self.iconView.image = [UIImage imageNamed:weibo.icon];
- // 2.昵称
- self.nameView.text = weibo.name;
- // 3.vip标志
- if (weibo.vip) {
- self.vipView.hidden = NO;
- }
- else {
- self.vipView.hidden = YES;
- }
- // 4.博文
- self.textView.text = weibo.text;
- // 5.配图
- if (weibo.picture) {
- self.pictureView.hidden = NO;
- self.pictureView.image = [UIImage imageNamed:weibo.picture];
- }
- else {
- self.pictureView.hidden = YES;
- self.pictureView.image = nil;
- }
- }
- // 设置位置、尺寸
- - (void) calWeiboFrame {
- // 1.头像
- self.iconView.frame = self.weiboFrame.iconFrame;
- // 2.昵称
- self.nameView.frame = self.weiboFrame.nameFrame;
- // 3.vip标志
- self.vipView.frame = self.weiboFrame.vipFrame;
- // 4.博文
- self.textView.frame = self.weiboFrame.textFrame;
- // 5.配图
- if (self.weiboFrame.weibo.picture) {
- self.pictureView.frame = self.weiboFrame.pictureFrame;
- }
- }
- @end
- //
- // Weibo.h
- //
- // Created by hellovoidworld on 14/12/5.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- // 装在微博数据的model
- #import <Foundation/Foundation.h>
- @interface Weibo : NSObject
- #pragma mark - 成员变量
- /** 头像 */
- @property(nonatomic, copy) NSString *icon;
- /** 昵称 */
- @property(nonatomic, copy) NSString *name;
- /** vip标志 */
- @property(nonatomic, assign) BOOL vip;
- /** 博文 */
- @property(nonatomic, copy) NSString *text;
- /** 配图 */
- @property(nonatomic, copy) NSString *picture;
- #pragma mark - 自定义初始化方法
- /** 使用字典赋值成员 */
- - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
- /** 使用字典赋值成员 */
- + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary;
- /** 返回空的model */
- + (instancetype) weibo;
- @end
- //
- // Weibo.m
- //
- // Created by hellovoidworld on 14/12/5.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- #import "Weibo.h"
- @implementation Weibo
- /** 使用字典赋值成员 */
- - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dictionary];
- }
- return self;
- }
- /** 使用字典赋值成员 */
- + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary {
- return [[self alloc] initWithDictionary:dictionary];
- }
- /** 返回空的model */
- + (instancetype) weibo {
- return [self weiboWithDictionary:nil];
- }
- @end
- //
- // WeiboFrame.h
- //
- // Created by hellovoidworld on 14/12/5.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- // 装在了每个cell的位置、尺寸和微博数据的model
- @class Weibo;
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h> // CGRect需要引入UIKit
- @interface WeiboFrame : NSObject
- // 微博数据
- @property(nonatomic, strong) Weibo *weibo;
- /** 头像 */
- @property(nonatomic, assign, readonly) CGRect iconFrame;
- /** 昵称 */
- @property(nonatomic, assign, readonly) CGRect nameFrame;
- /** vip标志 */
- @property(nonatomic, assign, readonly) CGRect vipFrame;
- /** 博文 */
- @property(nonatomic, assign, readonly) CGRect textFrame;
- /** 配图 */
- @property(nonatomic, assign, readonly) CGRect pictureFrame;
- /** 一条微博cell的高度 */
- @property(nonatomic, assign, readonly) CGFloat cellHeight;
- @end
- //
- // WeiboFrame.m
- //
- // Created by hellovoidworld on 14/12/5.
- // Copyright (c) 2014年 hellovoidworld. All rights reserved.
- //
- #import "WeiboFrame.h"
- #import "Weibo.h"
- // 昵称字体
- #define NAME_FONT [UIFont systemFontOfSize:14]
- // 博文字体
- #define TEXT_FONT [UIFont systemFontOfSize:15]
- @implementation WeiboFrame
- #pragma mark - 加载数据
- // 加载数据,用以计算各个控件的位置、尺寸
- - (void)setWeibo:(Weibo *)weibo {
- _weibo = weibo;
- // 间隙参数
- CGFloat padding = ;
- // 1.头像
- CGFloat iconWidth = ;
- CGFloat iconHeight = ;
- CGFloat iconX = padding;
- CGFloat iconY = padding;
- _iconFrame = CGRectMake(iconX, iconY, iconWidth, iconHeight);
- // 2.昵称
- // 计算昵称占用的size
- CGSize nameSize = [self calTextSizeWithText:self.weibo.name font:TEXT_FONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
- CGFloat nameX = CGRectGetMaxX(_iconFrame) + padding;
- CGFloat nameY = iconY + (iconHeight - nameSize.height) / ;// 居中
- _nameFrame.size = nameSize;
- _nameFrame.origin = CGPointMake(nameX, nameY);
- // 3.vip标志
- CGFloat vipWith = ;
- CGFloat vipHeight = ;
- CGFloat vipX = CGRectGetMaxX(_nameFrame) + padding;
- CGFloat vipY = nameY;
- _vipFrame = CGRectMake(vipX, vipY, vipWith, vipHeight);
- // 4.博文
- CGSize textSize = [self calTextSizeWithText:self.weibo.text font:TEXT_FONT maxSize:CGSizeMake(, MAXFLOAT)];
- CGFloat textX = padding;
- CGFloat textY = CGRectGetMaxY(_iconFrame) + padding;
- _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height);
- // 5.配图
- if (self.weibo.picture) {
- CGFloat pictureWidth = ;
- CGFloat pictureHeight = ;
- CGFloat pictureX = padding;
- CGFloat pictureY = CGRectGetMaxY(_textFrame) + padding;
- _pictureFrame = CGRectMake(pictureX, pictureY, pictureWidth, pictureHeight);
- _cellHeight = CGRectGetMaxY(_pictureFrame) + padding; //计算cell高度
- }
- else {
- _cellHeight = CGRectGetMaxY(_textFrame) + padding;
- }
- }
- // 使用自带方法计算一段文字占用的size
- - (CGSize) calTextSizeWithText:(NSString *) text font:(UIFont *) font maxSize:(CGSize) maxSize {
- NSDictionary *attrs = @{NSFontAttributeName : font};
- return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
- }
- @end


[iOS基础控件 - 6.7.1] 微博展示 代码的更多相关文章
- [iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引
A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代 ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- [iOS基础控件 - 6.7] 微博展示 使用代码自定义TableCell(动态尺寸)
A.需求 1.类似于微博内容的展示 2.头像 3.名字 4.会员标志 5.内容 6.分割线 7.配图(可选,可有可无) code source: https://github.com/hellov ...
- iOS 基础控件(下)
上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...
- [iOS基础控件 - 6.9] 聊天界面Demo
A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...
- [iOS基础控件 - 7.0] UIWebView
A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的 2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用 B.实现步骤 1.准备plist文件和国旗图片 2.创建模型 // // Flag.h // Co ...
- iOS基础 - 控件属性
一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...
随机推荐
- android 设置gridView item的高度
public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater ...
- jquery.cookie.js使用介绍
Cookies概述: Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术.Cookies是当你浏览某网站时,由Web服务器置于你硬盘上的一个非 ...
- 新的HTTP框架:Daraja Framework
https://www.habarisoft.com/daraja_framework.html
- WinAPI——钩子函数大全3
函数原形:LRESULT CALLBACK JournalPlaybackProc(int code, WPARAM wParam, LPARAM lParam); 参数: code:指示一个代码,被 ...
- JDK_Proxy_InvocationHandler_动态代理
本文用jdk动态代理模拟了spring的AOP的实现技术 AOP面向切面编程,可用于权限验证,效率检查,事务,异常管理等 JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Pr ...
- 宏FSP_SEG_INODES_PER_PAGE
#define FSP_SEG_INODES_PER_PAGE(zip_size) \ (((zip_size ? zip_size : UNIV_PAGE_SIZE) \ - FSEG_ARR_OF ...
- 取出block所对应的hash值
/**********************************************************************//** Gets the hash value of t ...
- 扫描.net dll引用dll
最近升级系统里的NHibernate,从3.3到4,项目工程太多, 一个模块分bll,dal,model,web,test,10几个模块,就要60多dll,升级一次太头疼. 编译过后,有时候会有的dl ...
- js不能执行,IE处理方法
一.如果你的ie不能打开js脚本(连系统里所有的js文件都不运行,网页上的js广告或好多页面都显示不了),请按一下步骤进行排查与解决: 1.查看是否IE的安全里面禁止了JS的运行: 将工具=>i ...
- Adobe RIA
一:1)Adobe® Flash® Player 是一个跨平台.基于浏览器的应用程序运行时,它可以跨屏幕和浏览器.原汁原味地呈现具有表现力的应用程序.内容和视频,当前版本Flash Player 10 ...