IOS第八天(4:UITableViewController新浪微博, 代码创建布局和数据转模型)

时间:2022-12-18 20:02:02

******控制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新浪微博, 代码创建布局和数据转模型)的更多相关文章

  1. IOS第八天&lpar;7&colon;UITableViewController新浪微博&comma;cell 复用的简单写法优化和cell高度从模型中获取&rpar;

    *********** #import "HMViewController.h" #import "HMStatus.h" #import "HMSt ...

  2. IOS第八天&lpar;6&colon;UITableViewController新浪微博&comma; 模型和 控件位置封装一起statusFrame&rpar;

    *****HMViewController #import "HMViewController.h" #import "HMStatus.h" #import ...

  3. IOS第八天&lpar;5&colon;UITableViewController新浪微博&comma; 计算行高&rpar;

    在 4 的 基础上重写 以下的方法 control #pragma mark - 代理方法 /** 计算单元格行高 */ - (CGFloat)tableView:(UITableView *)tab ...

  4. iOS UICollectionView&lpar;转一&rpar; XIB&plus;纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  5. IOS第八天&lpar;2&colon;UITableViewController团购&comma;点击底部&comma;xib加载更多&comma; 代理模式&rpar;

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  6. IOS第八天&lpar;3&colon;UITableViewController团购&comma; 点击底部代码调整&rpar;

    ****代理者的方法中 // 通知页脚视图调整视图显示状态 [footerView endRefresh]; //发送代理通知的类中 /** 视图控制器刷新完成调用方法 */ - (void)endR ...

  7. IOS第八天&lpar;1&colon;UITableViewController团购&comma;数据转模型&comma;xib显示数据&rpar;

    ******HMTg.h 模型数据 #import <Foundation/Foundation.h> @interface HMTg : NSObject @property (nona ...

  8. Android之代码创建布局

    大概描述一下效果:最外层是一个 RelativeLayout 里面有自定义个LinearLayout,每个LinearLayout有两个TextView.that's it !!! private v ...

  9. iOS UITableViewCell UITableVIewController 纯代码开发

    iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...

随机推荐

  1. HIS系统的处方录入控件

    前文<EFW框架Winform前端开发之强大的自定义控件库>中我们介绍了EFW框架中Winform系统中的自定义控件,本章使用自定义控件中的GridBoxCard控件开发了一个HIS系统的 ...

  2. PHP的&dollar;&lowbar;SERVER&lbrack;&&num;39&semi;HTTP&lowbar;HOST&&num;39&semi;&rsqb;获取服务器地址功能详解

    uchome的index文件中的二级域名功能判断,使用了php的$_SERVER['HTTP_HOST'],开始对这个不是很了解,所以百度了一下,发现一篇帖子有点意思,转发过来做个记录. 在php中, ...

  3. Android Framework------之Input子系统

    下面这是基于Android4.2代码的关于Input子系统的笔记.在这篇笔记中,只涉及Android相关的东西,关于Linux内核中对各种输入设备的统一,在本文中不作说明.此外,由于才疏学浅,文中难免 ...

  4. Windows Azure公有云服务相关方案

    http://www.cnblogs.com/sennly/p/4139675.html 1.公有云平台服务简介 Windows Azure 是一个灵活而开放的云平台,通过该平台,您可以在数据中心快速 ...

  5. 201421123042 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 答: 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计 ...

  6. MySQL 数据库的创建&amp&semi;修改

    -- 创建数据库 CREATE DATABASE [IF NOT EXISTS]<数据库名> DEFAULT CHARACTER SET utf8; -- 默认字符集为utf8 -- 指定 ...

  7. OLW Test

    第一篇使用Open Live Writer 发布测试. Code Insert: #include <stdio.h> main() { printf("Hello World! ...

  8. Javascript高级调试——console&period;table&lpar;&rpar;

    原文:http://www.mariusschulz.com/2013/11/13/advanced-javascript-debugging-with-consoletable 本文只是简单翻译,部 ...

  9. centos配置NTP服务器

    时间服务器: NTP(Network Time Protocol,网络时间协议)是用来使用网络中的各个计算机时间同步的一种协议,NTP服务器就是利用NTP协议提供时间同步服务的. 一.环境准备: 1. ...

  10. 南京邮电大学网络攻防平台WEB题

    平台地址:http://ctf.nuptsast.com/ 签到题: 右键查看源代码,得到flag md5 collision: 传入的a的md5值要为0e开头的,但不能是QNKCDZO,百度一个0e ...