IOS第八天(4:UITableViewController新浪微博, 代码创建布局和数据转模型)
******控制control
#import "HMViewController.h"
#import "HMStatus.h"
#import "HMStatusCell.h" @interface HMViewController ()
@property (nonatomic, strong) NSArray *statuses;
@end @implementation HMViewController - (NSArray *)statuses
{
if (_statuses == nil) _statuses = [HMStatus statuses];
return _statuses;
} - (void)viewDidLoad
{
[super viewDidLoad]; self.tableView.rowHeight = ;
} #pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statuses.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell";
HMStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) {
cell = [[HMStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 赋值
cell.status = self.statuses[indexPath.row]; return cell;
} #pragma mark - 代理方法
/** 计算单元格行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/**
计算行高的方法,会在加载表格数据时,有多少行计算多少次 contentSize 问题:此方法执行的时候,cell还没有被实例化!
但是:行高计算是在实例化cell时,通过设置status属性,计算的=>有了status模型,就可以知道行高! 问题:如何在cell实例化之前,获得行高?
解决方法:通过status可以计算得到行高!=》再建立一个模型,专门计算所有控件的位置
*/
return ;
}
******modol
#import <Foundation/Foundation.h> @interface HMStatus : NSObject @property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *picture;
@property (nonatomic, assign) BOOL vip; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)statusWithDict:(NSDictionary *)dict; + (NSArray *)statuses; @end
***** HMStatus.m文件
#import "HMStatus.h" @implementation HMStatus - (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)statusWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} + (NSArray *)statuses
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
[arrayM addObject:[self statusWithDict:dict]];
} return arrayM;
} @end
*****cell
#import <UIKit/UIKit.h>
@class HMStatus; @interface HMStatusCell : UITableViewCell
@property (nonatomic, strong) HMStatus *status;
@end
cell m文件
#import "HMStatusCell.h"
#import "HMStatus.h" /** 姓名字体 */
#define kNameFont [UIFont systemFontOfSize:14]
/** 正文字体 */
#define kTextFont [UIFont systemFontOfSize:16] @interface HMStatusCell() @property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameView;
@property (nonatomic, strong) UIImageView *vipView;
@property (nonatomic, strong) UILabel *textView;
@property (nonatomic, strong) UIImageView *pictureView; @end @implementation HMStatusCell - (UIImageView *)iconView
{
if (_iconView == nil) {
_iconView = [[UIImageView alloc] init];
[self.contentView addSubview:_iconView];
}
return _iconView;
} - (UILabel *)nameView
{
if (_nameView == nil) {
_nameView = [[UILabel alloc] init];
// 默认字体是17号
_nameView.font = kNameFont;
[self.contentView addSubview:_nameView];
}
return _nameView;
} - (UIImageView *)vipView
{
if (_vipView == nil) {
_vipView = [[UIImageView alloc] init];
_vipView.image = [UIImage imageNamed:@"vip"];
_vipView.hidden = YES; [self.contentView addSubview:_vipView];
}
return _vipView;
} - (UILabel *)textView
{
if (_textView == nil) {
_textView = [[UILabel alloc] init];
_textView.font = kTextFont;
_textView.numberOfLines = ; [self.contentView addSubview:_textView];
}
return _textView;
} - (UIImageView *)pictureView
{
if (_pictureView == nil) {
_pictureView = [[UIImageView alloc] init];
[self.contentView addSubview:_pictureView];
}
return _pictureView;
} - (void)setStatus:(HMStatus *)status
{
_status = status; // 1> 设置数据
[self settingData]; // 2> 设置位置
[self settingFrame];
} /** 设置数据 */
- (void)settingData
{
// 头像
self.iconView.image = [UIImage imageNamed:self.status.icon];
// 姓名
self.nameView.text = self.status.name;
// vip(可选的)
if (self.status.vip) {
self.vipView.hidden = NO;
self.nameView.textColor = [UIColor redColor];
} else {
self.vipView.hidden = YES;
self.nameView.textColor = [UIColor blackColor];
} // 正文
self.textView.text = self.status.text; // 配图(可选参数)
// imageNamed:nil CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
if (self.status.picture.length > ) {
self.pictureView.hidden = NO;
self.pictureView.image = [UIImage imageNamed:self.status.picture];
} else {
self.pictureView.hidden = YES;
}
} /** 设置位置 */
- (void)settingFrame
{
// 0. 定义间距
CGFloat padding = ; // 1. 头像
CGFloat iconX = padding;
CGFloat iconY = padding;
CGFloat iconW = ;
CGFloat iconH = ;
self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH); // 2. 姓名大小由文字的长度来决定
// boundingRectWithSize计算给定文本字符串所占的区域
// 返回值是一个x,y = 0的CGRect,w,h是计算好的宽高
//
// 如果要计算多行的准确高度,需要传入NSStringDrawingUsesLineFragmentOrigin选项
// dict用于指定字体的相关属性的字典,UIKit框架中的第一个头文件
// context: nil
NSDictionary *nameDict = @{NSFontAttributeName: kNameFont};
CGRect nameFrame = [self.status.name boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameDict context:nil];
nameFrame.origin.x = CGRectGetMaxX(self.iconView.frame) + padding;
nameFrame.origin.y = padding + (self.iconView.bounds.size.height - nameFrame.size.height) * 0.5;
self.nameView.frame = nameFrame; // vip图标
CGFloat vipX = CGRectGetMaxX(self.nameView.frame) + padding;
CGFloat vipY = self.nameView.frame.origin.y;
CGFloat vipW = ;
CGFloat vipH = ;
self.vipView.frame = CGRectMake(vipX, vipY, vipW, vipH); // 正文
NSDictionary *textDict = @{NSFontAttributeName: kTextFont};
CGRect textFrame = [self.status.text boundingRectWithSize:CGSizeMake(, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil];
textFrame.origin.x = padding;
textFrame.origin.y = CGRectGetMaxY(self.iconView.frame) + padding;
self.textView.frame = textFrame; CGFloat cellHeight; if (self.status.picture.length > ) {
// 配图
CGFloat pictureX = padding;
CGFloat pictureY = CGRectGetMaxY(textFrame) + padding;
CGFloat pictureW = ;
CGFloat pictureH = ;
self.pictureView.frame = CGRectMake(pictureX, pictureY, pictureW, pictureH); cellHeight = CGRectGetMaxY(self.pictureView.frame) + padding;
} else {
cellHeight = CGRectGetMaxY(self.textView.frame) + padding;
}
} @end
IOS第八天(4:UITableViewController新浪微博, 代码创建布局和数据转模型)的更多相关文章
- IOS第八天(7:UITableViewController新浪微博,cell 复用的简单写法优化和cell高度从模型中获取)
*********** #import "HMViewController.h" #import "HMStatus.h" #import "HMSt ...
- IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)
*****HMViewController #import "HMViewController.h" #import "HMStatus.h" #import ...
- IOS第八天(5:UITableViewController新浪微博, 计算行高)
在 4 的 基础上重写 以下的方法 control #pragma mark - 代理方法 /** 计算单元格行高 */ - (CGFloat)tableView:(UITableView *)tab ...
- iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距
之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...
- IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)
******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...
- IOS第八天(3:UITableViewController团购, 点击底部代码调整)
****代理者的方法中 // 通知页脚视图调整视图显示状态 [footerView endRefresh]; //发送代理通知的类中 /** 视图控制器刷新完成调用方法 */ - (void)endR ...
- IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
******HMTg.h 模型数据 #import <Foundation/Foundation.h> @interface HMTg : NSObject @property (nona ...
- Android之代码创建布局
大概描述一下效果:最外层是一个 RelativeLayout 里面有自定义个LinearLayout,每个LinearLayout有两个TextView.that's it !!! private v ...
- iOS UITableViewCell UITableVIewController 纯代码开发
iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...
随机推荐
- hdu5057 Argestes and Sequence 分块
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Submiss ...
- DELPHI与C#语法比较
1.我做了三年的.NET,也是三个月前因为项目需要转的delphi整个过渡差不多要一周到两周.正常情况两周后就能熟悉delphi.delphi可以调整开发环境的,你把他的属性和解决方案窗口调成和你用V ...
- Qt-note0906
1.在Qt中每一个类都有一个与其同名的头文件: 2.任何一个Qt GUI程序都要有一个QApplication对象: 3.在默认情况下,新建的可视部件对象都是不可见的,要使用show()函数让它们显示 ...
- Service Provider模式
参考文章:[http://blog.csdn.net/zl3450341/article/details/7227197] Service Interface:服务接口,将服务通过抽象统一声明,供客户 ...
- Shell 编程基础之 Case 练习
一.语法 case $变量 in "第一个变量内容") # 每个变量内容建议用双引号括起来,关键字则为小括号 ) # 执行内容 ;; # 每个类别结尾使用两个连续的分号来处理! & ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 1147. Shaping Regions
1147. Shaping Regions Time limit: 0.5 secondMemory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) o ...
- BZOJ3946 : 无聊的游戏
首先把所有串拼起来,后插入的串在前面,得到一个大串. 那么任意时刻,每个串是由这个大串的若干个不相交的子串从左到右拼接而成. 用线段树维护每个串,每个节点维护一个标记,表示区间内的串要加上什么前缀. ...
- 使用TCMalloc优化OpenResty
1.安装依赖包 yum -y install wget gcc gcc-c++ -y 2.安装libunwind库可以从http://ftp.twaren.net/Unix/NonGNU//libun ...
- Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_GiftAnimationView"
1> error 详情: Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_GiftAnimationView&quo ...