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的更多相关文章
随机推荐
- Web前端之jQuery 的10大操作技巧
不管是做什么事情,人们习惯在工作中去找方法.找技巧,来帮助提高效率,在软件开发中更是如此.jQuery作为前端开发必学技术之一,在使用中也有各种各样的小技巧,今天小编为大家分享10条必知会的技巧,希望 ...
- ios常用的第三方库
ios开发中有可能用到的第三方库进行记录一下: 注:资料信息来源于网络 自己整理 https://developer.apple.com/reference(苹果官方文档) https://gith ...
- python中的collections
python中有大量的内置模块,很多是属于特定开发的功能性模块,但collections是属于对基础数据的类型的补充模块,因此,在日常代码中使用频率更高一些,值得做个笔记,本文只做主要关键字介绍,详细 ...
- Python 判断字符串是否为数字
转载: http://www.runoob.com/python3/python3-check-is-number.html 以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为 ...
- Java基础学习(四)
流程控制 /* 控制流程语句之---if 判断语句 格式一: 只适用于一种情况下去使用. if(判断条件){ 符合条件执行的代码; } 格式二:适用于两种情况下去使用 if(判断条件){ 符合条件执行 ...
- byte[] 转十进制
short s = 0; //一个16位整形变量,初值为 0000 0000 0000 0000 byte b1 = 1; //一个byte的变量,作为转换后的高8位,假设初值为 0000 0001 ...
- strtok源码 bitset 空间压缩
源代码里有一段: unsigned char map[32]; /* Clear control map */ for (count = 0; count < 32; count++) map[ ...
- 题目:求1+2+…+n,
题目:求1+2+-+n, 要求不能使用乘除法.for.while.if.else.switch.case等关键字 以及条件判断语句(A?B:C). java 实现 public class sum { ...
- Hibernate 非常见异常集合
异常一:org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity ...
- h5容易遗忘的内容
1.表单中 input类型 小补充: 2.常用的表单元素 3.表单属性 4.表单事件 5.多媒体:音频和视频 5.1音频 5.2视频 6.Dom拓展