標籤:
/ ---------------------- 功能分區 -----------------------------
// 到這步,想要實現在自訂的每個tablecell上添加一個toolbar的view,上面放著三個lable(點贊,評論,轉寄的數量),所以要再自訂一個view類
// 自訂的 LYWeiboToolbarView 在xib裡右側功能區點擊第一個表徵圖把Use Autolayout自動布局點空取消,之後在尺寸的一欄給距離俯視圖的下邊緣加約束,距離為0.(實現toolbar跟隨cell的高度變換位置,始終在下邊緣)
//cell上添加工具列
// 建立自訂通過xib布局的對象,用下面的方法建立
LYWeiboToolbarView *tv = [[[NSBundlemainBundle]loadNibNamed:@"LYWeiboToolbarView" owner:self options:nil] lastObject];
// autoreseing布局,只是實現子控制項的每次初始位置,這裡給設定向下移5個像素
tv.center = CGPointMake(tv.center.x, tv.center.y+5);
//在這裡要把LYWeibo資料傳遞給子控制項tv,tv拿到後要顯示轉寄,評論,點贊數量,但是LYWeibo的資料的時間(具體的時間是在controller裡賦值LYWeibo的時候)是在下面的setWeibo方法裡拿到的
[self.contentView addSubview:tv];
// 設定 contentView 層的顏色為白色,區分整體cell的灰色,分割效果
self.contentView.backgroundColor = [UIColor whiteColor];
self.backgroundColor = [UIColor lightGrayColor];
self.toolBarView = tv;
}
return self;
}
// 重寫屬性weibo的set方法
-(void)setWeibo:(LYWeibo *)weibo{
_weibo = weibo;
self.nameLabel.text = weibo.user.name;
self.timeLabel.text = weibo.created_at;
[self.headIV setImageWithURL:weibo.user.avatar_large];
self.sourceLabel.text = weibo.source;
//告訴bar顯示的資料是什麼(轉寄。。。)
self.toolBarView.weibo = weibo;
}
//控制項顯示之前會調用 資料準備就緒 修改自訂控制項布局 可以寫在次方法中
-(void)layoutSubviews{
[super layoutSubviews];
// 讓ContentView高度減10 做分割線
self.contentView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-10);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
}
// ---------------------- 功能分區 -----------------------------
// 擷取到的時間格式:
//@"Wed Feb 24 16:03:33 +0800 2016"
//星期幾 月份 號 時:分:秒 時區 年
// ---------------------- 功能分區 -----------------------------
// 每次重新整理微博的時候,顯示的時間是根據發送的時間而定的,所以這裡不能重寫set方法,需要重寫他的get方法
-(NSString *)created_at{
// 擷取微博發送時間
// 把獲得的字串時間 轉成 時間戳記
//EEE(星期) MMM(月份)dd(天) HH小時 mm分鐘 ss秒 Z時區 yyyy年
// NSDateFormatter 是一個時間互相轉換的類
NSDateFormatter *format = [[NSDateFormatter alloc]init];
// 格式化時間的格式,按照這種格式的時間把時間轉換成data
format.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
//設定地區
format.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
// 把時間資料轉換成data資料
NSDate *weiboDate = [format dateFromString:_created_at];
//擷取目前時間
NSDate *nowDate = [NSDate new];
long nowTime = [nowDate timeIntervalSince1970];
long weiboTime = [weiboDate timeIntervalSince1970];
long time = nowTime-weiboTime;
if (time<60) {//一分鐘內 顯示剛剛
return @"剛剛";
}else if (time>60&&time<=3600){
return [NSString stringWithFormat:@"%d分鐘前",(int)time/60];
}else if (time>3600&&time<3600*24){
return [NSString stringWithFormat:@"%d小時前",(int)time/3600];
}else{//直接顯示日期
// 按照MM月dd的格式把微博data時間轉換成顯示的的時間
format.dateFormat = @"MM月dd";
return [format stringFromDate:weiboDate];
}
}
藍懿ios微博項目之發送資訊