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的更多相关文章
随机推荐
- Html 5 Web Storage
HTML5 中使用Web Storage 技术进行本地存储,能够在Web 客户端进行数据存储.WebStorage 曾今属于HTML5的规范,目前已经被独立出来形成单独的规范体系.简单来说使用Web本 ...
- ATM
package duzhaonan;import java.util.Scanner;import javax.swing.JOptionPane;class Account{//创建的账户类 Str ...
- $\mathscr{F}$类
$\mathscr{F}$类:在单位元盘$B(0,1)$中满足$$f(0)=0,f'(0)=1$$ 的双全纯函数的全体.
- Toad for Sqlserver
# 设置制表符 从sqlserver拷贝的存储过程粘贴到Toad,代码变得不整齐了,这就需要设置下制表符的大小.
- 【NodeJS】环境变量配置
安装完Node后,NodeJS自带npm.于是我照着网上的教程想搭一个脚手架.结果报错: ’node’ 不是内部或外部命令,也不是可运行的程序 但是我检查了一下系统环境变量,path底下有正确引用no ...
- .Net缓存管理框架CacheManager(转)
转载地址:http://www.cnblogs.com/JustRun1983/p/CacheManager.html Cache缓存在计算机领域是一个被普遍使用的概念.硬件中CPU有一级缓存,二级缓 ...
- Android中用TextView显示大量文字的方法
最近学习Android中,试着实现一个简单的显示新闻Demo的时候,遇到了一个问题:一条新闻的内容文字很多,放在TextView上面超出屏幕了,怎么破? 查了一下资料,找到了两种方法实现: 1. 只用 ...
- java中的throw与throws的区别
什么时运行时异常?什么是非运行时异常? 通俗的讲: 运行时异常:就是编译通过,运行时就崩了,比如数组越界. 非运行时异常:就是编译不通过,这时就得必须去处理了.不然就没法运行了. 全面的讲: Thro ...
- 连接Mysql提示Can’t connect to local MySQL server through socket的解决方法
mysql,mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL se ...
- SVN
你先在本地把包删了,然后提交上级目录就可以了.比如你要删除com.aaa.A这个类.你就把A删了,然后提交aaa就行,如果你要把aaa包删了,你就删了以后提交com这个包.SVN就删掉了 svn提交更 ...