IOS第八天(4:UITableViewController新浪微博, 代码创建布局和数据转模型)
2015-08-11 16:36
405 查看
******控制control
******modol
***** HMStatus.m文件
*****cell
cell m文件
#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 = 200; } #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 200; }
******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 = 0; [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 > 0) { self.pictureView.hidden = NO; self.pictureView.image = [UIImage imageNamed:self.status.picture]; } else { self.pictureView.hidden = YES; } } /** 设置位置 */ - (void)settingFrame { // 0. 定义间距 CGFloat padding = 10; // 1. 头像 CGFloat iconX = padding; CGFloat iconY = padding; CGFloat iconW = 30; CGFloat iconH = 30; 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 = 14; CGFloat vipH = 14; self.vipView.frame = CGRectMake(vipX, vipY, vipW, vipH); // 正文 NSDictionary *textDict = @{NSFontAttributeName: kTextFont}; CGRect textFrame = [self.status.text boundingRectWithSize:CGSizeMake(300, 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 > 0) { // 配图 CGFloat pictureX = padding; CGFloat pictureY = CGRectGetMaxY(textFrame) + padding; CGFloat pictureW = 100; CGFloat pictureH = 100; self.pictureView.frame = CGRectMake(pictureX, pictureY, pictureW, pictureH); cellHeight = CGRectGetMaxY(self.pictureView.frame) + padding; } else { cellHeight = CGRectGetMaxY(self.textView.frame) + padding; } } @end
相关文章推荐
- UI初识
- Easyui 表格datagrid 的单元格内容显示方式扩展
- Easy UI的树形菜单的异步加载
- FlexBuilder远程调试WEB应用
- 控件学习---UIPageControl---摘自培训资料
- Halton Sequence 原理和代码实现
- 23-IO流-51-IO流(序列流-SequenceInputStream-枚举和迭代)
- 23-IO流-50-IO流(序列流-SequenceInputStream)
- UIScrollView或者UITableView 点击状态栏,回不到顶部的解决办法
- Druid数据库连接池的简单使用
- poj 3715 Blue and Red(二分图最大匹配+字典序输出)
- iOS基础-UIKit框架-高级视图-UIPickerView-实例2:城市选择(列与列之间有关系)
- Map四种获取key和value值的方法,以及对map中的元素排序
- 让JPA的Query返回Map对象
- 225 Implement Stack using Queues
- Hadoop中的序列化:自定义value类型
- UIMenuController的使用
- HDU 4991 Ordered Subsequence
- 【Android UI】FragmentTabHost 实现底部菜单
- UI023---代码实现UITableView