Controller:
  1. //
  2. // ViewController.m
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/4.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "Weibo.h"
  11. #import "WeiboCell.h"
  12. #import "WeiboFrame.h"
  13.  
  14. @interface ViewController ()
  15.  
  16. /** 微博数组,类型是WeiboFrame,包含了数据和位置尺寸信息 */
  17. @property(nonatomic, strong) NSArray *weibos;
  18.  
  19. @end
  20.  
  21. @implementation ViewController
  22.  
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view, typically from a nib.
  26. }
  27.  
  28. - (void)didReceiveMemoryWarning {
  29. [super didReceiveMemoryWarning];
  30. // Dispose of any resources that can be recreated.
  31. }
  32.  
  33. // 屏蔽状态栏
  34. - (BOOL)prefersStatusBarHidden {
  35. return YES;
  36. }
  37.  
  38. #pragma mark - 数据源操作
  39. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  40. return self.weibos.count;
  41. }
  42.  
  43. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  44. // 传入tableView是为了使用cell缓存池
  45. WeiboCell *cell = [WeiboCell cellWithTableView:self.tableView];
  46.  
  47. // 传入微博的数据和位置尺寸信息
  48. cell.weiboFrame = self.weibos[indexPath.row];
  49.  
  50. return cell;
  51. }
  52.  
  53. #pragma mark - 加载数据
  54. // 延迟加载plist文件中的数据为微博数组
  55. - (NSArray *) weibos {
  56. if (nil == _weibos) {
  57. NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];
  58.  
  59. NSMutableArray *mdictArray = [NSMutableArray array];
  60. for (NSDictionary *dict in dictArray) {
  61. WeiboFrame *weiboFrame = [[WeiboFrame alloc] init];
  62. Weibo *weibo = [Weibo weiboWithDictionary:dict];
  63.  
  64. // 传入weibo模型数据到frame模型,内部保存数据,计算各个控件的位置、尺寸
  65. weiboFrame.weibo = weibo;
  66.  
  67. [mdictArray addObject:weiboFrame];
  68. }
  69.  
  70. _weibos = mdictArray;
  71. }
  72.  
  73. return _weibos;
  74. }
  75.  
  76. #pragma mark - 代理操作
  77. // 动态调整每个cell的高度
  78. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  79. WeiboFrame *weiboFrame = self.weibos[indexPath.row];
  80. return weiboFrame.cellHeight;
  81. }
  82.  
  83. @end
 
View:
  1. //
  2. // WeiboCell.h
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/5.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. // 用于装载每个TabelViewCell的model
  10. #import <UIKit/UIKit.h>
  11.  
  12. @class WeiboFrame;
  13. @interface WeiboCell : UITableViewCell
  14.  
  15. // 微博frame,内持有微博数据和尺寸、位置信息
  16. @property(nonatomic, strong) WeiboFrame *weiboFrame;
  17.  
  18. // 自定义带有父控件tableView初始化方法
  19. + (instancetype) cellWithTableView:(UITableView *) tableView;
  20.  
  21. @end
 
  1. //
  2. // WeiboCell.m
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/5.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "WeiboCell.h"
  10. #import "WeiboFrame.h"
  11. #import "Weibo.h"
  12.  
  13. // 昵称字体
  14. #define NAME_FONT [UIFont systemFontOfSize:14]
  15. // 博文字体
  16. #define TEXT_FONT [UIFont systemFontOfSize:15]
  17.  
  18. @interface WeiboCell()
  19.  
  20. // 创建各个子控件的成员,用来分离数据赋值和尺寸、位置调整
  21. /** 头像 */
  22. @property(nonatomic, weak) UIImageView *iconView;
  23.  
  24. /** 昵称 */
  25. @property(nonatomic, weak) UILabel *nameView;
  26.  
  27. /** vip标志 */
  28. @property(nonatomic, weak) UIImageView *vipView;
  29.  
  30. /** 博文 */
  31. @property(nonatomic, weak) UILabel *textView;
  32.  
  33. /** 配图 */
  34. @property(nonatomic, weak) UIImageView *pictureView;
  35.  
  36. @end
  37.  
  38. @implementation WeiboCell
  39.  
  40. - (void)awakeFromNib {
  41. // Initialization code
  42. }
  43.  
  44. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  45. [super setSelected:selected animated:animated];
  46.  
  47. // Configure the view for the selected state
  48. }
  49.  
  50. #pragma mark - 初始化
  51. // 自定义带有父控件tableView初始化方法
  52. + (instancetype) cellWithTableView:(UITableView *) tableView {
  53. static NSString *ID = @"weibo";
  54.  
  55. // 从缓存池寻找
  56. WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  57.  
  58. // 使用重写的构造方法初始化
  59. if (nil == cell) {
  60. cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
  61. }
  62.  
  63. return cell;
  64. }
  65.  
  66. // 重写缓存池初始化方法,加入各个子控件,可以设置静态数据,但是没有动态的数据和位置尺寸信息
  67. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  68. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  69. // 1.头像
  70. /**
  71. 由于self.iconView是weak类型,不能写成:
  72. self.iconView = [[UIImageView alloc] init];
  73. 会被立即释放,不能正常赋值,下同
  74. */
  75. UIImageView *iconView = [[UIImageView alloc] init];
  76. [self.contentView addSubview:iconView];
  77. self.iconView = iconView;
  78.  
  79. // 2.昵称
  80. UILabel *nameView = [[UILabel alloc] init];
  81. // 指定字体用来计算占用的尺寸大小
  82. nameView.font = NAME_FONT;
  83. [self.contentView addSubview:nameView];
  84. self.nameView = nameView;
  85.  
  86. // 3.vip标志
  87. UIImageView *vipView = [[UIImageView alloc] init];
  88. vipView.image = [UIImage imageNamed:@"vip"];
  89. [self.contentView addSubview:vipView];
  90. self.vipView = vipView;
  91.  
  92. // 4.博文
  93. UILabel *textView = [[UILabel alloc] init];
  94. textView.font = TEXT_FONT;
  95. textView.numberOfLines = ;// 设置自动换行
  96. [self.contentView addSubview:textView];
  97. self.textView = textView;
  98.  
  99. // 5.配图
  100. UIImageView *pictureView = [[UIImageView alloc] init];
  101. [self.contentView addSubview:pictureView];
  102. self.pictureView = pictureView;
  103. }
  104.  
  105. return self;
  106. }
  107.  
  108. #pragma mark - 数据加载
  109. // 加载数据的时候设置数据和尺寸、位置
  110. - (void)setWeiboFrame:(WeiboFrame *)weiboFrame {
  111. _weiboFrame = weiboFrame;
  112.  
  113. // 1.设置数据
  114. [self calWeiboData];
  115.  
  116. // 2.设置尺寸、位置
  117. [self calWeiboFrame];
  118. }
  119.  
  120. // 设置数据
  121. - (void) calWeiboData {
  122. Weibo *weibo = self.weiboFrame.weibo;
  123.  
  124. // 1.头像
  125. self.iconView.image = [UIImage imageNamed:weibo.icon];
  126.  
  127. // 2.昵称
  128. self.nameView.text = weibo.name;
  129.  
  130. // 3.vip标志
  131. if (weibo.vip) {
  132. self.vipView.hidden = NO;
  133. }
  134. else {
  135. self.vipView.hidden = YES;
  136. }
  137.  
  138. // 4.博文
  139. self.textView.text = weibo.text;
  140.  
  141. // 5.配图
  142. if (weibo.picture) {
  143. self.pictureView.hidden = NO;
  144. self.pictureView.image = [UIImage imageNamed:weibo.picture];
  145. }
  146. else {
  147. self.pictureView.hidden = YES;
  148. self.pictureView.image = nil;
  149. }
  150. }
  151.  
  152. // 设置位置、尺寸
  153. - (void) calWeiboFrame {
  154. // 1.头像
  155. self.iconView.frame = self.weiboFrame.iconFrame;
  156.  
  157. // 2.昵称
  158. self.nameView.frame = self.weiboFrame.nameFrame;
  159.  
  160. // 3.vip标志
  161. self.vipView.frame = self.weiboFrame.vipFrame;
  162.  
  163. // 4.博文
  164. self.textView.frame = self.weiboFrame.textFrame;
  165.  
  166. // 5.配图
  167. if (self.weiboFrame.weibo.picture) {
  168. self.pictureView.frame = self.weiboFrame.pictureFrame;
  169. }
  170. }
  171.  
  172. @end
 
 
Model:
  1. //
  2. // Weibo.h
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/5.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. // 装在微博数据的model
  10. #import <Foundation/Foundation.h>
  11.  
  12. @interface Weibo : NSObject
  13.  
  14. #pragma mark - 成员变量
  15. /** 头像 */
  16. @property(nonatomic, copy) NSString *icon;
  17.  
  18. /** 昵称 */
  19. @property(nonatomic, copy) NSString *name;
  20.  
  21. /** vip标志 */
  22. @property(nonatomic, assign) BOOL vip;
  23.  
  24. /** 博文 */
  25. @property(nonatomic, copy) NSString *text;
  26.  
  27. /** 配图 */
  28. @property(nonatomic, copy) NSString *picture;
  29.  
  30. #pragma mark - 自定义初始化方法
  31. /** 使用字典赋值成员 */
  32. - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
  33.  
  34. /** 使用字典赋值成员 */
  35. + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary;
  36.  
  37. /** 返回空的model */
  38. + (instancetype) weibo;
  39.  
  40. @end
 
  1. //
  2. // Weibo.m
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/5.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "Weibo.h"
  10.  
  11. @implementation Weibo
  12.  
  13. /** 使用字典赋值成员 */
  14. - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
  15. if (self = [super init]) {
  16. [self setValuesForKeysWithDictionary:dictionary];
  17. }
  18.  
  19. return self;
  20. }
  21.  
  22. /** 使用字典赋值成员 */
  23. + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary {
  24. return [[self alloc] initWithDictionary:dictionary];
  25. }
  26.  
  27. /** 返回空的model */
  28. + (instancetype) weibo {
  29. return [self weiboWithDictionary:nil];
  30. }
  31.  
  32. @end
 
  1. //
  2. // WeiboFrame.h
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/5.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. // 装在了每个cell的位置、尺寸和微博数据的model
  10.  
  11. @class Weibo;
  12. #import <Foundation/Foundation.h>
  13. #import <UIKit/UIKit.h> // CGRect需要引入UIKit
  14.  
  15. @interface WeiboFrame : NSObject
  16.  
  17. // 微博数据
  18. @property(nonatomic, strong) Weibo *weibo;
  19.  
  20. /** 头像 */
  21. @property(nonatomic, assign, readonly) CGRect iconFrame;
  22.  
  23. /** 昵称 */
  24. @property(nonatomic, assign, readonly) CGRect nameFrame;
  25.  
  26. /** vip标志 */
  27. @property(nonatomic, assign, readonly) CGRect vipFrame;
  28.  
  29. /** 博文 */
  30. @property(nonatomic, assign, readonly) CGRect textFrame;
  31.  
  32. /** 配图 */
  33. @property(nonatomic, assign, readonly) CGRect pictureFrame;
  34.  
  35. /** 一条微博cell的高度 */
  36. @property(nonatomic, assign, readonly) CGFloat cellHeight;
  37.  
  38. @end
  1. //
  2. // WeiboFrame.m
  3. // Weibo
  4. //
  5. // Created by hellovoidworld on 14/12/5.
  6. // Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "WeiboFrame.h"
  10. #import "Weibo.h"
  11.  
  12. // 昵称字体
  13. #define NAME_FONT [UIFont systemFontOfSize:14]
  14. // 博文字体
  15. #define TEXT_FONT [UIFont systemFontOfSize:15]
  16.  
  17. @implementation WeiboFrame
  18.  
  19. #pragma mark - 加载数据
  20. // 加载数据,用以计算各个控件的位置、尺寸
  21. - (void)setWeibo:(Weibo *)weibo {
  22. _weibo = weibo;
  23.  
  24. // 间隙参数
  25. CGFloat padding = ;
  26.  
  27. // 1.头像
  28. CGFloat iconWidth = ;
  29. CGFloat iconHeight = ;
  30. CGFloat iconX = padding;
  31. CGFloat iconY = padding;
  32. _iconFrame = CGRectMake(iconX, iconY, iconWidth, iconHeight);
  33.  
  34. // 2.昵称
  35. // 计算昵称占用的size
  36. CGSize nameSize = [self calTextSizeWithText:self.weibo.name font:TEXT_FONT maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
  37.  
  38. CGFloat nameX = CGRectGetMaxX(_iconFrame) + padding;
  39. CGFloat nameY = iconY + (iconHeight - nameSize.height) / ;// 居中
  40. _nameFrame.size = nameSize;
  41. _nameFrame.origin = CGPointMake(nameX, nameY);
  42.  
  43. // 3.vip标志
  44. CGFloat vipWith = ;
  45. CGFloat vipHeight = ;
  46. CGFloat vipX = CGRectGetMaxX(_nameFrame) + padding;
  47. CGFloat vipY = nameY;
  48. _vipFrame = CGRectMake(vipX, vipY, vipWith, vipHeight);
  49.  
  50. // 4.博文
  51. CGSize textSize = [self calTextSizeWithText:self.weibo.text font:TEXT_FONT maxSize:CGSizeMake(, MAXFLOAT)];
  52. CGFloat textX = padding;
  53. CGFloat textY = CGRectGetMaxY(_iconFrame) + padding;
  54. _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height);
  55.  
  56. // 5.配图
  57. if (self.weibo.picture) {
  58. CGFloat pictureWidth = ;
  59. CGFloat pictureHeight = ;
  60. CGFloat pictureX = padding;
  61. CGFloat pictureY = CGRectGetMaxY(_textFrame) + padding;
  62. _pictureFrame = CGRectMake(pictureX, pictureY, pictureWidth, pictureHeight);
  63.  
  64. _cellHeight = CGRectGetMaxY(_pictureFrame) + padding; //计算cell高度
  65. }
  66. else {
  67. _cellHeight = CGRectGetMaxY(_textFrame) + padding;
  68. }
  69. }
  70.  
  71. // 使用自带方法计算一段文字占用的size
  72. - (CGSize) calTextSizeWithText:(NSString *) text font:(UIFont *) font maxSize:(CGSize) maxSize {
  73. NSDictionary *attrs = @{NSFontAttributeName : font};
  74.  
  75. return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
  76. }
  77.  
  78. @end
 
weibo.plist:
 
images:
 
 
 
 
 
 

[iOS基础控件 - 6.7.1] 微博展示 代码的更多相关文章

  1. [iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引

    A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代 ...

  2. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

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

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

  4. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  5. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

  6. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

  7. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

  8. [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo

    A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用   B.实现步骤 1.准备plist文件和国旗图片     2.创建模型 // // Flag.h // Co ...

  9. iOS基础 - 控件属性

    一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...

随机推荐

  1. android 设置gridView item的高度

    public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater ...

  2. jquery.cookie.js使用介绍

    Cookies概述: Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术.Cookies是当你浏览某网站时,由Web服务器置于你硬盘上的一个非 ...

  3. 新的HTTP框架:Daraja Framework

    https://www.habarisoft.com/daraja_framework.html

  4. WinAPI——钩子函数大全3

    函数原形:LRESULT CALLBACK JournalPlaybackProc(int code, WPARAM wParam, LPARAM lParam); 参数: code:指示一个代码,被 ...

  5. JDK_Proxy_InvocationHandler_动态代理

    本文用jdk动态代理模拟了spring的AOP的实现技术 AOP面向切面编程,可用于权限验证,效率检查,事务,异常管理等 JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Pr ...

  6. 宏FSP_SEG_INODES_PER_PAGE

    #define FSP_SEG_INODES_PER_PAGE(zip_size) \ (((zip_size ? zip_size : UNIV_PAGE_SIZE) \ - FSEG_ARR_OF ...

  7. 取出block所对应的hash值

    /**********************************************************************//** Gets the hash value of t ...

  8. 扫描.net dll引用dll

    最近升级系统里的NHibernate,从3.3到4,项目工程太多, 一个模块分bll,dal,model,web,test,10几个模块,就要60多dll,升级一次太头疼. 编译过后,有时候会有的dl ...

  9. js不能执行,IE处理方法

    一.如果你的ie不能打开js脚本(连系统里所有的js文件都不运行,网页上的js广告或好多页面都显示不了),请按一下步骤进行排查与解决: 1.查看是否IE的安全里面禁止了JS的运行: 将工具=>i ...

  10. Adobe RIA

    一:1)Adobe® Flash® Player 是一个跨平台.基于浏览器的应用程序运行时,它可以跨屏幕和浏览器.原汁原味地呈现具有表现力的应用程序.内容和视频,当前版本Flash Player 10 ...