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的更多相关文章
随机推荐
- Oracle中使用REGEXP_SUBSTR,regexp_replace函数
REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)__srcstr ...
- nodeJS(express4.x)+vue(vue-cli)构建前后端分离详细教程(带跨域)
好想再回到大学宿舍,当时床虽小,房随小,但是心确是满的 ----致 西安工程大学a-114舍友们 转载请注明出处:水车:http://www.cnblogs.com/xuange306/p/6185 ...
- C++根据图片url下载图片
需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...
- Hamilton四元数群的表示
Hamilton四元数群$Q_8=\mathbb H=\{\pm e,\pm i,\pm j,\pm k\}$满足如下运算法则: $e$为单位元且同号得正.异号得负,此外$e=i^2=j^2=k^2, ...
- Linux中的用户和用户组
在Linux中,有三种用户: Root 用户:也称为超级用户,对系统拥有完全的控制权限.超级用户可以不受限制的运行任何命令.Root 用户可以看做是系统管理员. 系统用户:系统用户是Linux运行 ...
- C# oracle odp.net 32位/64位版本的问题
问题如下: 系统是win7 64位,技术 asp.net mvc 4, 数据库 oracle 11g. 由于某些原因只能使用 32的 ODP.NET ( Oracle Data Provider ), ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Jquery便利对象
xhr=[object object] $.each(xhr, function (key, val) { alert(key + '=' + val); ...
- 优化Web中的性能
优化Web中的性能 简介 web的优化就是一场阻止http请求最终访问到数据库的战争. 优化的方式就是加缓存,在各个节点加缓存. web请求的流程及节点 熟悉流程及节点,才能定位性能的问题.而且优化的 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...