一 段子的下拉
建立模型
数字类型的用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 = 35; CGFloat const BSTitlesViewY = 64;
titlesView.height = BSTitlesViewH;
titlesView.y = BSTitlesViewY;
3 可以在numberOfRowsInSection中判断刷新控件的初始状态(是否被隐藏)
self.tableView.mj_footer.hidden = self.topics.count == 0;
四 cell的基本结构
1 取消分割线
//取消分割线 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.backgroundColor = [UIColor clearColor];
2 设置图片不要拉伸
3 重写cell的间距
- (void)setFrame:(CGRect)frame { static CGFloat margin = 10; frame.origin.x = margin; frame.size.width -= 2 * 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 200; }
五 cell的基本数据
处理按钮的文字
- (void)setupButtonTitle:(UIButton *)button count:(NSInteger)count placeholder:(NSString *)placeholder { if (count == 0) { placeholder = placeholder; }else if (count > 10000) { 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:0]; 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:0]; } @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:0]; return cmps.year == 0 && cmps.month == 0 && cmps.day == 1; }
测试:
八 日期的精确显示
利用刚才的事件处理方法获得精确的不同的时间处理数据,并显示在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) {//时间差距 >= 1小时 return [NSString stringWithFormat:@"%zd小时前", cmps.hour]; }else if(cmps.minute >= 1){//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(100) % 2;
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 @"29"; }
2 将变量作为父类中的一个属性,在调用时直接传入参数
/* 帖子类型 */ @property(nonatomic, copy)NSString *type;
BSTopicViewController *word = [[BSTopicViewController alloc] init]; word.title = @"段子"; word.type = @"29"; [self addChildViewController:word];
3 将变量类型写成枚举
typedef enum { BSTopicTypeAll = 1, BSTopicTypePicture = 10, BSTopicTypeWord = 29, BSTopicTypeVoice = 31, BSTopicTypeVideo = 41 } BSTopicType;
/* 帖子类型 */ @property(nonatomic, assign) BSTopicType type;
BSTopicViewController *word = [[BSTopicViewController alloc] init]; word.title = @"段子"; word.type = BSTopicTypeWord; [self addChildViewController:word];