BSBuDeJie_04
一 段子的下拉
建立模型
数字类型的用assign
/* 当前页码 */
@property (nonatomic, assign) NSInteger page;
二 下拉上拉细节处理
三 细节处理
1 在各种网络之下,page的数值也不同
2 定义全局常量
.h文件放引用
.m文件放定义
command + b 编译一下
#import <UIKit/UIKit.h> UIKIT_EXTERN CGFloat const BSTitlesViewH;
UIKIT_EXTERN CGFloat const BSTitlesViewY;
#import <UIKit/UIKit.h> CGFloat const BSTitlesViewH = ;
CGFloat const BSTitlesViewY = ;
titlesView.height = BSTitlesViewH;
titlesView.y = BSTitlesViewY;
3 可以在numberOfRowsInSection中判断刷新控件的初始状态(是否被隐藏)
self.tableView.mj_footer.hidden = self.topics.count == ;
四 cell的基本结构
1 取消分割线
//取消分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor clearColor];
2 设置图片不要拉伸

3 重写cell的间距
- (void)setFrame:(CGRect)frame
{
static CGFloat margin = ; frame.origin.x = margin;
frame.size.width -= * frame.origin.x;
frame.size.height -= margin;
frame.origin.y += margin; [super setFrame:frame];
}
4 在cell中使用Model
@class BSTopic; @interface BSTopicCell : UITableViewCell /* 模型数据 */
@property (nonatomic, strong) BSTopic *topic; @end
- (void)setTopic:(BSTopic *)topic
{
_topic = topic;
}
cell.topic = self.topics[indexPath.row];
5 设置cell的背景图片
- (void)awakeFromNib
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"mainCellBackground"];
self.backgroundView = imageView;
}
6 利用代理方法设置cell的高度
# pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ return ;
}

五 cell的基本数据
处理按钮的文字
- (void)setupButtonTitle:(UIButton *)button count:(NSInteger)count placeholder:(NSString *)placeholder
{ if (count == ) {
placeholder = placeholder;
}else if (count > ) {
placeholder = [NSString stringWithFormat:@"%.1f万", count / 10000.0];
}else{
placeholder = [NSString stringWithFormat:@"%zd", count];
} [button setTitle:placeholder forState:UIControlStateNormal];
}

六
1 处理时间数据
用服务器返回的时间与当前时间对比
//当前时间
NSDate *now = [NSDate date];
//发帖时间 NSString -> NSDate
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//设置日期格式(y:年 M:月 d:天 H:小时 m:分钟 s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *create = [fmt dateFromString:topic.create_time]; NSTimeInterval delta = [create timeIntervalSinceDate:now]; NSLog(@"%f",delta);

2 NSCalendar日历类
2.1 利用NSCalendar来获取NSDate的每一个元素
//当前时间
NSDate *now = [NSDate date]; //日历
NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger year = [calendar component:NSCalendarUnitYear fromDate:now];
NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:now];
NSInteger day = [calendar component:NSCalendarUnitDay fromDate:now];
BSLog(@"%zd",year);

2.2
//当前时间
NSDate *now = [NSDate date]; //日历
NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *cmps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:now]; BSLog(@"%zd %zd %zd ", cmps.year, cmps.month, cmps.day);

3 比较时间
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//设置日期格式(y:年 M:月 d:天 H:小时 m:分钟 s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; //当前时间
NSDate *now = [NSDate date]; NSDate *create = [fmt dateFromString:create_time]; //日历
NSCalendar *calendar = [NSCalendar currentCalendar]; //比较时间
NSCalendarUnit unit = NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar components:unit fromDate:create toDate:now options:]; BSLog(@"%@ %@", now, create);
BSLog(@"%zd %zd %zd %zd %zd %zd", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);

+0000东八区
4 封装到扩展类中
#import <Foundation/Foundation.h> @interface NSDate (BSExtension) //比较from和self的时间差值
- (NSDateComponents *)deltaFrom:(NSDate *)from; @end
#import "NSDate+BSExtension.h" @implementation NSDate (BSExtension) - (NSDateComponents *)deltaFrom:(NSDate *)from
{
//日历
NSCalendar *calendar = [NSCalendar currentCalendar]; //比较时间
NSCalendarUnit unit = NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
return [calendar components:unit fromDate:from toDate:self options:]; } @end

七 日期处理
@interface NSDate (BSExtension) //比较from和self的时间差值
- (NSDateComponents *)deltaFrom:(NSDate *)from; //是否为今年
- (BOOL)isThisYear;
//是否为今天
- (BOOL)isToday;
//是否为昨天
- (BOOL)isYesteday; @end
- (BOOL)isThisYear
{
//日历
NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self]; return nowYear == selfYear;
} //- (BOOL)isToday
//{
// //日历
// NSCalendar *calendar = [NSCalendar currentCalendar];
//
// NSCalendarUnit unit = NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay;
// NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
//
// return nowCmps.year == selfCmps.year && nowCmps.month == selfCmps.month && nowCmps.day == selfCmps.day;
//} - (BOOL)isToday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd"; NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self]; return [nowString isEqualToString:selfString];
} - (BOOL)isYesteday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd"; NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]]; //日历
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:]; return cmps.year == && cmps.month == && cmps.day == ; }
测试:
八 日期的精确显示
利用刚才的事件处理方法获得精确的不同的时间处理数据,并显示在cell中
/*
今年
今天
1分钟内
刚刚
1小时内
xx分钟前
其他
xx小时前
昨天
昨天 12:00:00
其他
10-10 12:00:00
非今年
2016-10-10 12:22:22
*/
- (NSString *)create_time{
//日期格式化
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//设置日期格式(y:年 M:月 d:天 H:小时 m:分钟 s:秒)
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *create = [fmt dateFromString:_create_time];
if (create.isThisYear) {//今年
if (create.isToday) {//今天
NSDateComponents *cmps = [[NSDate date] deltaFrom:create];
if (cmps.hour >= ) {//时间差距 >= 1小时
return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
}else if(cmps.minute >= ){//1小时 > 时间差距 >= 1分钟
return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
}else{//时间差距 < 1分钟
return @"刚刚";
}
}else if (create.isYesteday){//昨天
fmt.dateFormat = @"昨天 HH:mm:ss";
return [fmt stringFromDate:create];
}else{//其他
fmt.dateFormat = @"MM-dd HH:mm:ss";
return [fmt stringFromDate:create];
}
}else{//非今年
return _create_time;
}
}

九 新浪加V
1 随机生成二进制数字
//随机生成的二进制数字
topic.sina_v = arc4random_uniform() % ;
2 控制视图是否隐藏(根据服务器返回的值)
//新浪加V
self.sinaVView.hidden = !topic.isSina_v;
3 把服务器返回的int转成bool类型
/* 是否是新浪加V用户 */
@property (nonatomic, assign, getter=isSina_v) BOOL sina_v;


十 子控制器重构(变量在父类中的处理)
1 可以在父类中声明一个方法,然后在子类中实现
/* 帖子类型(交给子类去实现) */
- (NSString *)type;
- (NSString *)type
{
return @"";
}
2 将变量作为父类中的一个属性,在调用时直接传入参数
/* 帖子类型 */
@property(nonatomic, copy)NSString *type;
BSTopicViewController *word = [[BSTopicViewController alloc] init];
word.title = @"段子";
word.type = @"";
[self addChildViewController:word];
3 将变量类型写成枚举
typedef enum
{
BSTopicTypeAll = ,
BSTopicTypePicture = ,
BSTopicTypeWord = ,
BSTopicTypeVoice = ,
BSTopicTypeVideo = } BSTopicType;
/* 帖子类型 */
@property(nonatomic, assign) BSTopicType type;
BSTopicViewController *word = [[BSTopicViewController alloc] init];
word.title = @"段子";
word.type = BSTopicTypeWord;
[self addChildViewController:word];
BSBuDeJie_04的更多相关文章
随机推荐
- 订货(bzoj 2424)
Description 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初的库存量为零,第n月月底的库存量也为 ...
- 了解一下OOP的反射API
PHP5的类和对象函数并没有告诉我们类内部的所有一切,而只是报告了它们的公共成员.要充分了解一个类,需要知道其私有成员和保护成员,还要知道其方法所期望的参数 .对此,使用反射API. 1 查看自定义类 ...
- user agent stylesheet (浏览器默认样式)!
"-webkit-margin-before". "-webkit-margin-after". "-webkit-margin-start" ...
- 记录一下折腾webp 的过程
最近有客户想要处理webp 的动图,情况当然是我们并不能处理webp 格式的图片.这事就交给了我来折腾,一开始想着用瑞士军刀ffmpeg.结果是折腾了差不多一天,前前后后编译了几十次ffmpeg 源码 ...
- JavaScript call
<script> function dog() { this.sound = "wangwang~"; this.shout = function () { alert ...
- Oracle临时文件
临时数据文件时一种特殊的文件,当内存不足时,Oracle用他来存储一些临时数据,如排序或散列操作. 自12c起,对临时表的操作所产生的undo也会放到临时表空间中,而在12c之前,这部分undo放在u ...
- vue-loader配合webpack的使用及安装
vue-loader配合webpack的使用及安装: 工程文件简单的目录结构 index.html main.js 入口文件 App.vue vue文件,官方推荐命名法 package.jso ...
- 微信小程序简介
什么是微信小程序? 今年下半年的时候,微信推出了微信小程序,当然刚刚推出来的时候还是处于内测阶段,但是这并不影响这家伙的热度,也许这是一个新的时代的开启.但是什么是微信小程序呢?微信应用号是一个app ...
- [转]在Eclipse中使用JUnit4进行单元测试(初级篇)
首先,我们来一个傻瓜式速成教程,不要问为什么,Follow Me,先来体验一下单元测试的快感! 首先新建一个项目叫JUnit_Test,我们编写一个Calculator类,这是一个能够简单实现加减乘除 ...
- iTerm 2 && Oh My Zsh
一年前,在搞终端的时候偶然一次机会,让我看到了各种强大的DIY界面,这让我很想去自己搞一个.于是在网上不断的寻找资源,也请教了大多数朋友.最终以失败告终.最近,本人又突然想起当时这件事,于是,决定边做 ...