一 概述

本文介绍实现新浪微博的基本页面效果,代码结果如下:

  • Status:模型数据

  • StatusFrame:Cell中每个组件要显示的内容和位置

  • StatusCell:自定义Cell

  • ViewController:页面控制器

<!--more-->

二 页面效果图

三 代码

3.1 Status

Status.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;
​
@end

Status.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];
}
@end

3.2 StatusFrame

StatusFrame.h

#import <Foundation/Foundation.h>
@class HMStatus;
@interface HMStatusFrame : NSObject
@property (nonatomic, assign, readonly) CGRect nameF;
@property (nonatomic, assign, readonly) CGRect iconF;
@property (nonatomic, assign, readonly) CGRect textF;
@property (nonatomic, assign, readonly) CGRect pictureF;
@property (nonatomic, assign, readonly) CGRect vipF;
/** 单元格高度 */
@property (nonatomic, assign, readonly) CGFloat cellHeight;
​
@property (nonatomic, strong) HMStatus *status;
​
+ (NSArray *)statusFrames;
​
@end

StatusFrame.m

#import "HMStatusFrame.h"
#import "HMStatus.h"
#import "NSString+Tools.h"
​
/** 姓名字体 */
#define kNameFont   [UIFont systemFontOfSize:14]
/** 正文字体 */
#define kTextFont   [UIFont systemFontOfSize:16]
​
@implementation HMStatusFrame
​
- (void)setStatus:(HMStatus *)status
{_status = status;// 控件之间的间距CGFloat padding = 10;// 1> 头像CGFloat iconX = padding;CGFloat iconY = padding;CGFloat iconW = 30;CGFloat iconH = 30;_iconF = CGRectMake(iconX, iconY, iconW, iconH);// 2> 名字NSDictionary *nameDict = @{NSFontAttributeName: kNameFont};CGRect nameRect = [self.status.name textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:nameDict];nameRect.origin.x = CGRectGetMaxX(self.iconF) + padding;nameRect.origin.y = iconY + (iconH - nameRect.size.height) * 0.5;_nameF = nameRect;// 3> vipCGFloat vipX = CGRectGetMaxX(self.nameF) + padding;CGFloat vipY = nameRect.origin.y;CGFloat vipW = 14;CGFloat vipH = 14;_vipF = CGRectMake(vipX, vipY, vipW, vipH);// 4> 文字NSDictionary *textDict = @{NSFontAttributeName: kTextFont};CGRect textRect = [self.status.text textRectWithSize:CGSizeMake(300, MAXFLOAT) attributes:textDict];textRect.origin.x = padding;textRect.origin.y = CGRectGetMaxY(self.iconF) + padding;_textF = textRect;// 5> 图像if (self.status.picture.length > 0) {CGFloat pictureX = padding;CGFloat pictureY = CGRectGetMaxY(self.textF);CGFloat pictureW = 100;CGFloat pictureH = 100;_pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);_cellHeight = CGRectGetMaxY(self.pictureF) + padding;} else {_cellHeight = CGRectGetMaxY(self.textF) + padding;}
}
+ (NSArray *)statusFrames
{NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]];NSMutableArray *arrayM = [NSMutableArray array];for (NSDictionary *dict in array) {HMStatusFrame *statusFrame = [[HMStatusFrame alloc] init];statusFrame.status = [HMStatus statusWithDict:dict];[arrayM addObject:statusFrame];}return [arrayM copy];
}
@end

3.3 StatusCell

StatusCell.h

#import <UIKit/UIKit.h>
@class HMStatusFrame;
​
@interface HMStatusCell : UITableViewCell
@property (nonatomic, strong) HMStatusFrame *statusFrame;
@end

StatusCell.m

#import "HMStatusCell.h"
#import "HMStatus.h"
#import "HMStatusFrame.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
​
#pragma mark - getter方法,创建控件
- (UIImageView *)iconView
{if (_iconView == nil) {_iconView = [[UIImageView alloc] init];[self.contentView addSubview:_iconView];}return _iconView;
}
​
- (UILabel *)nameView
{if (_nameView == nil) {_nameView = [[UILabel alloc] init];_nameView.font = kNameFont;[self.contentView addSubview:_nameView];}return _nameView;
}
​
- (UIImageView *)vipView
{if (_vipView == nil) {_vipView = [[UIImageView alloc] init];self.vipView.image = [UIImage imageNamed:@"vip"];[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;
}
​
#pragma mark - setter方法,设置模型
- (void)setStatusFrame:(HMStatusFrame *)statusFrame
{_statusFrame = statusFrame;[self settingData];[self settingsFrame];
}
​
/** 设置数据 */
- (void)settingData
{HMStatus *status = self.statusFrame.status;self.iconView.image = [UIImage imageNamed:status.icon];self.nameView.text = status.name;self.vipView.hidden = (status.vip == 0);if (self.vipView.hidden) {self.nameView.textColor = [UIColor blackColor];} else {self.nameView.textColor = [UIColor redColor];}self.textView.text = status.text;self.pictureView.hidden = (status.picture.length == 0);if (!self.pictureView.hidden) {self.pictureView.image = [UIImage imageNamed:status.picture];}
}
​
/** 设置位置 */
- (void)settingsFrame
{// 1> 头像self.iconView.frame = self.statusFrame.iconF;// 2> 名字self.nameView.frame = self.statusFrame.nameF;// 3> vipself.vipView.frame = self.statusFrame.vipF;// 4> 文字self.textView.frame = self.statusFrame.textF;// 5> 图像if (self.statusFrame.status.picture.length > 0) {self.pictureView.frame = self.statusFrame.pictureF;}
}
@end

3.4 ViewControllser.m

#import "HMViewController.h"
#import "HMStatus.h"
#import "HMStatusCell.h"
#import "HMStatusFrame.h"
​
@interface HMViewController ()
@property (nonatomic, strong) NSArray *statusFrames;
@end
​
@implementation HMViewController
static NSString *ID = @"Cell";
​
- (NSArray *)statusFrames
{if (_statusFrames == nil) _statusFrames = [HMStatusFrame statusFrames];return _statusFrames;
}
​
- (void)viewDidLoad
{[super viewDidLoad];// 通过代码为表格注册可重用单元格[self.tableView registerClass:[HMStatusCell class] forCellReuseIdentifier:ID];
}
​
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return self.statusFrames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{HMStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];cell.statusFrame = self.statusFrames[indexPath.row];return cell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{HMStatusFrame *statusFrame = self.statusFrames[indexPath.row];return statusFrame.cellHeight;
}
@end

IOS开发之——新浪微博(42)相关推荐

  1. IOS开发之新浪微博OAuth2

    说明:微博开放接口的调用,如发微博.关注等,都是需要获取用户身份认证的.目前微博开放平台用户身份鉴权主要采用的是OAuth2.0.为了方便开发者开发.测试自己的应用. OAuth2.0较1.0相比,整 ...

  2. IOS开发——新浪微博认证失败处理

    IOS开发--新浪微博认证失败处理 认证失败错误提示: 请求返回错误( error.errorDescription):Insufficient app permissions! 1,检查 分享集成部 ...

  3. iOS开发三方资源 - 欲先攻其事必先利其器

    一:源代码实例 1:快速搭建项目源代码 地址:https://github.com/wujunyang/MobileProject MobileProject项目是一个以MVC模式搭建的开源功能集合, ...

  4. iOS开发长文--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: ...

  5. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook详解

    代码改变世界 Posts - 69, Articles - 0, Comments - 812 Cnblogs Dashboard Login Home Contact Gallery RSS Ken ...

  6. iOS开发 --- 开发工具

    一:源代码实例 1:快速搭建项目源代码 地址:https://github.com/wujunyang/MobileProject 2:高仿美团iOS版 地址:https://github.com/l ...

  7. iOS开发--一些开源的学习资源

    vim插件: https://github.com/Valloric/YouCompleteMe vim插件配置: https://github.com/spf13/spf13-vim ------- ...

  8. 【最火的ios开发技术】常用工具和ios常用开源框架库

    youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配 ...

  9. iOS开发指南:从零基础到App Store上架(第2版 )

    第一部分 基础篇 第1章 开篇综述 2 1.1 iOS概述 2 1.1.1 iOS介绍 2 1.1.2 iOS 6新特性 2 1.2 开发环境及开发工具 3 1.3 本书中的约定 4 1.3.1 案例 ...

最新文章

  1. 主流的Python领域和框架--转
  2. C语言求最大公约数与最小公倍数
  3. linux内核镜像sd卡,【原创】Linux QT镜像的制作--制作SD卡启动盘
  4. mysql计算相邻的差值_MySQL如何计算相邻两行某列差值
  5. 在才开始进入前端这个坑的时候 在布局中会遇到很多问题 我才入这个坑的时候 在margin top 中遇到几个bug 我分享一下...
  6. 剑指offer面试题[41]-和为s的两个数VS和为s的连续正数序列
  7. java常用快捷键 智能提示 及快捷键冲突
  8. C# WPF中DataGrid的数据绑定(Binding)
  9. Appium Server
  10. OpenGL 编程指南 ( 原书第 9 版 ) --- 第一章
  11. 使用cboard(oracle数据库)
  12. 课设-基于51单片机+超声波模块的避障小车(源码+原理图+Protel仿真)
  13. 微软认知服务应用秘籍 – 漫画翻译篇
  14. xml文件导入wps_怎么用wpsExcel表打开xml文档
  15. debian的几个lib源_Linux各个文件夹的主要作用 (源地址
  16. 【哈希表】(一) 设计哈希表
  17. Oracle EBS GL日记账批“选定以过账”状态数据修复
  18. linux系统下回收站,Linux/Unix回收站(trash)
  19. 酷家乐母公司群核科技冲刺美股上市:2020年亏损约3亿元,曾因违规多次被通报
  20. IntelliJ IDEA中文插件

热门文章

  1. 极路由1S 开启临时ssh 并刷breed成功
  2. 分治与减治算法实验:题目6 淘汰赛冠军问题
  3. 远程PLC监控调试,PLC通用中转服务器,多客户端tcp中转服务器源代码
  4. Revit2016 笔记05
  5. 企业电子招标采购系统源码
  6. 每秒解析千兆字节的JSON解析器开源,秒杀一大波解析器!
  7. 【FPGA数控】基于FPGA的小型步进电机数控装置的设计与实现
  8. Jsp中htmlEscape=false是什么意思
  9. java中m三个等号是什么意思_三个等号是什么意思
  10. 常用的操作系统镜像下载windows、linux 和MacOS