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

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
54 #pragma mark - 加载数据
55 // 延迟加载plist文件中的数据为微博数组
56 - (NSArray *) weibos {
57     if (nil == _weibos) {
58         NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];
59
60         NSMutableArray *mdictArray = [NSMutableArray array];
61         for (NSDictionary *dict in dictArray) {
62             WeiboFrame *weiboFrame = [[WeiboFrame alloc] init];
63             Weibo *weibo = [Weibo weiboWithDictionary:dict];
64
65             // 传入weibo模型数据到frame模型,内部保存数据,计算各个控件的位置、尺寸
66             weiboFrame.weibo = weibo;
67
68             [mdictArray addObject:weiboFrame];
69         }
70
71         _weibos = mdictArray;
72     }
73
74     return _weibos;
75 }
76
77
78 #pragma mark - 代理操作
79 // 动态调整每个cell的高度
80 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
81     WeiboFrame *weiboFrame = self.weibos[indexPath.row];
82     return weiboFrame.cellHeight;
83 }
84
85 @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
19 // 自定义带有父控件tableView初始化方法
20 + (instancetype) cellWithTableView:(UITableView *) tableView;
21
22 @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
 19 @interface WeiboCell()
 20
 21 // 创建各个子控件的成员,用来分离数据赋值和尺寸、位置调整
 22 /** 头像 */
 23 @property(nonatomic, weak) UIImageView *iconView;
 24
 25 /** 昵称 */
 26 @property(nonatomic, weak) UILabel *nameView;
 27
 28 /** vip标志 */
 29 @property(nonatomic, weak) UIImageView *vipView;
 30
 31 /** 博文 */
 32 @property(nonatomic, weak) UILabel *textView;
 33
 34 /** 配图 */
 35 @property(nonatomic, weak) UIImageView *pictureView;
 36
 37 @end
 38
 39 @implementation WeiboCell
 40
 41 - (void)awakeFromNib {
 42     // Initialization code
 43 }
 44
 45 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
 46     [super setSelected:selected animated:animated];
 47
 48     // Configure the view for the selected state
 49 }
 50
 51 #pragma mark - 初始化
 52 // 自定义带有父控件tableView初始化方法
 53 + (instancetype) cellWithTableView:(UITableView *) tableView {
 54     static NSString *ID = @"weibo";
 55
 56     // 从缓存池寻找
 57     WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
 58
 59     // 使用重写的构造方法初始化
 60     if (nil == cell) {
 61         cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
 62     }
 63
 64     return  cell;
 65 }
 66
 67 // 重写缓存池初始化方法,加入各个子控件,可以设置静态数据,但是没有动态的数据和位置尺寸信息
 68 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 69     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
 70         // 1.头像
 71         /**
 72             由于self.iconView是weak类型,不能写成:
 73             self.iconView = [[UIImageView alloc] init];
 74             会被立即释放,不能正常赋值,下同
 75          */
 76         UIImageView *iconView = [[UIImageView alloc] init];
 77         [self addSubview:iconView];
 78         self.iconView = iconView;
 79
 80         // 2.昵称
 81         UILabel *nameView = [[UILabel alloc] init];
 82         // 指定字体用来计算占用的尺寸大小
 83         nameView.font = NAME_FONT;
 84         [self addSubview:nameView];
 85         self.nameView = nameView;
 86
 87         // 3.vip标志
 88         UIImageView *vipView = [[UIImageView alloc] init];
 89         vipView.image = [UIImage imageNamed:@"vip"];
 90         [self addSubview:vipView];
 91         self.vipView = vipView;
 92
 93         // 4.博文
 94         UILabel *textView = [[UILabel alloc] init];
 95         textView.font = TEXT_FONT;
 96         textView.numberOfLines = 0;// 设置自动换行
 97         [self addSubview:textView];
 98         self.textView = textView;
 99
100         // 5.配图
101         UIImageView *pictureView = [[UIImageView alloc] init];
102         [self addSubview:pictureView];
103         self.pictureView = pictureView;
104     }
105
106     return self;
107 }
108
109 #pragma mark - 数据加载
110 // 加载数据的时候设置数据和尺寸、位置
111 - (void)setWeiboFrame:(WeiboFrame *)weiboFrame {
112     _weiboFrame = weiboFrame;
113
114     // 1.设置数据
115     [self calWeiboData];
116
117     // 2.设置尺寸、位置
118     [self calWeiboFrame];
119 }
120
121 // 设置数据
122 - (void) calWeiboData {
123     Weibo *weibo = self.weiboFrame.weibo;
124
125     // 1.头像
126     self.iconView.image = [UIImage imageNamed:weibo.icon];
127
128     // 2.昵称
129     self.nameView.text = weibo.name;
130
131     // 3.vip标志
132     if (weibo.vip) {
133         self.vipView.hidden = NO;
134     }
135     else {
136         self.vipView.hidden = YES;
137     }
138
139     // 4.博文
140     self.textView.text = weibo.text;
141
142
143     // 5.配图
144     if (weibo.picture) {
145         self.pictureView.hidden = NO;
146         self.pictureView.image = [UIImage imageNamed:weibo.picture];
147     }
148     else {
149         self.pictureView.hidden = YES;
150         self.pictureView.image = nil;
151     }
152 }
153
154 // 设置位置、尺寸
155 - (void) calWeiboFrame {
156     // 1.头像
157     self.iconView.frame = self.weiboFrame.iconFrame;
158
159     // 2.昵称
160     self.nameView.frame = self.weiboFrame.nameFrame;
161
162     // 3.vip标志
163     self.vipView.frame = self.weiboFrame.vipFrame;
164
165     // 4.博文
166     self.textView.frame = self.weiboFrame.textFrame;
167
168     // 5.配图
169     if (self.weiboFrame.weibo.picture) {
170         self.pictureView.frame = self.weiboFrame.pictureFrame;
171     }
172 }
173
174
175 @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
31 #pragma mark - 自定义初始化方法
32 /** 使用字典赋值成员 */
33 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
34
35 /** 使用字典赋值成员 */
36 + (instancetype) weiboWithDictionary:(NSDictionary *) dictionary;
37
38 /** 返回空的model */
39 + (instancetype) weibo;
40
41 @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
39 @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 = 10;
26
27     // 1.头像
28     CGFloat iconWidth = 30;
29     CGFloat iconHeight = 30;
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) / 2;// 居中
40     _nameFrame.size = nameSize;
41     _nameFrame.origin = CGPointMake(nameX, nameY);
42
43     // 3.vip标志
44     CGFloat vipWith = 14;
45     CGFloat vipHeight = 14;
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(300, 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 = 100;
59         CGFloat pictureHeight = 100;
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
79  

weibo.plist:

images:

时间: 2024-08-06 06:35:23

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

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

A.需求 1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model 2.使用KVC进行Model封装赋值 3.展示头字母标题 4.展示索引(使用KVC代替遍历取出所有索引值) B.实现 1.Model嵌套 其实就是将另一个Model作为成员 .plist 文件结构 GroupCar中有存储Car的数组 1 @property(nonatomic, strong) NSArray *cars; 封装Model的时候需要进行相应处理 CarGroup.

iOS基础控件UINavigationController中的传值

iOS基础控件UINavigationController中的传值,代理传值,正向传值,反向传值 #import <UIKit/UIKit.h> //声明一个协议 @protocol SendValue<NSObject> //定义一个方法 - (void)sendBtnTitle:(NSString *)title; @end @interface FirstViewController : UIViewController // 定义代理 @property (nonatomi

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

A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不可以再按 2.在屏幕中间弹出一个消息框,通知消息“xx已经被安装”,慢慢消失 3.消息框样式为圆角半透明 B.不使用代理模式,使用app空间组和主View之间的父子View关系 1.在主View中创建一个消息框 主View控制器:ViewController.m 1 // 创建下载成功消息框 2 CGFloat labelWid

IOS Ui控件 修改位置和尺寸,代码添加控件

所有的UI控件最终都继承自UIView,UI控件的公共属性都定义在UIView中, UIView的常见属性 UIView *superview; 获得自己的父控件对象 NSArray *subviews; 获得自己的所有子控件对象 NSInteger tag; 控件的ID(标识),父控件可以通过tag来找到对应的子控件 CGAffineTransform transform; 控件的形变属性(可以设置旋转角度.比例缩放.平移等属性) CGRect frame; 控件所在矩形框在父控件中的位置和尺

ios基础控件之开关按钮(UISwitch)

UISwitch控件是iOS开发的基础控件,是非常简单的一个控件,因为它的方法比较少.UISwitch继承于UIControl基类,因此可以当成活动控件使用. 注意:开关状态通过它的on属性进行读取,该属性是一个BOOL属性 创建: UISwitch* mySwitch = [[ UISwitch alloc]initWithFrame:CGRectMake(0.150.0f,100.0f,0.0f,0.0f)]; 可能你会疑问为什么它的大小都设置为0?没错,它的大小你设置是无效的,系统会为你分

[iOS基础控件 - 6.6.1] 展示团购数据代码[iOS基础控件 - 6.7] 微博展示 使用代码自定义TableCell(动态尺寸)

A.需求 1.类似于微博内容的展示 2.头像 3.名字 4.会员标志 5.内容 6.分割线 7.配图(可选,可有可无) B.思路.步骤 1.Controller:UITableViewController 改变控制器继承自UITableViewController,storyboard中也同时使用新的TableViewController,其TableView作为启动入口. 2.View:代码自定义cell 使用代码组装每个cell,动态改变控件的位置.尺寸 cell含有一个WeiboFrame

iOS 基础控件(下)

上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知道这个是和滚动相关的控件,在Android开发时遇到过ScrollView,当内容的尺寸超出了屏幕范围之后,用ScrollView则可以通过滚动的方式使得超出屏幕的那部分内容通过滚动的方式显示出来,在Android里面有水平的ScrollView和垂直的ScrollView,在iOS里面就只有一个S

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

A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话框 4.隐藏相同的发送时间 5.底部功能按钮:语音按钮.消息输入框.表情按钮.附加按钮 6.响应键盘事件,呼出键盘.隐藏键盘时对上述的视图作出上移操作 7.键盘的发送事件处理 B.实现点 1.底层视图搭建 上部分聊天信息框:UITableView 下部分功能区:UIButton 信息输入框使用无边框

【iOS基础控件 - 11】【Demo】模仿qq ,微信 UI 聊天界面

A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话框 4.隐藏相同的发送时间 5.底部功能按钮:语音按钮.消息输入框.表情按钮.附加按钮 6.响应键盘事件,呼出键盘.隐藏键盘时对上述的视图作出上移操作 7.键盘的发送事件处理 Code Source: B.实现点 1.底层视图搭建 上部分聊天信息框:UITableView 下部分功能区:UIButt