Browse Source

Add interface for setting updatedTimeTitle

Add interface for setting updatedTimeTitle
MJLee 10 years ago
parent
commit
74c7b1282e

+ 2 - 2
MJRefresh.podspec

@@ -1,12 +1,12 @@
 Pod::Spec.new do |s|
   s.name         = "MJRefresh"
-  s.version      = "1.2.0"
+  s.version      = "1.3.0"
   s.summary      = "The easiest way to use pull-to-refresh"
   s.homepage     = "https://github.com/CoderMJLee/MJRefresh"
   s.license      = "MIT"
   s.authors      = { 'MJ Lee' => '199109106@qq.com'}
   s.platform     = :ios, "6.0"
-  s.source       = { :git => "https://github.com/CoderMJLee/MJRefresh.git", :tag => "1.2.0" }
+  s.source       = { :git => "https://github.com/CoderMJLee/MJRefresh.git", :tag => "1.3.0" }
   s.source_files = "MJRefreshExample/MJRefreshExample/MJRefresh/*.{h,m}"
   s.resource     = "MJRefreshExample/MJRefreshExample/MJRefresh/MJRefresh.bundle"
   s.requires_arc = true

+ 3 - 0
MJRefreshExample/MJRefreshExample/MJRefresh/MJRefreshHeader.h

@@ -23,6 +23,9 @@ typedef enum {
 /** 利用这个key来保存上次的刷新时间(不同界面的刷新控件应该用不同的dateKey,以区分不同界面的刷新时间) */
 @property (copy, nonatomic) NSString *dateKey;
 
+/** 利用这个block来决定显示的更新时间 */
+@property (copy, nonatomic) NSString *(^updatedTimeTitle)(NSDate *updatedTime);
+
 /**
  * 设置state状态下的状态文字内容title(别直接拿stateLabel修改文字)
  */

+ 16 - 6
MJRefreshExample/MJRefreshExample/MJRefresh/MJRefreshHeader.m

@@ -115,24 +115,34 @@ - (void)layoutSubviews
 }
 
 #pragma mark - 私有方法
+- (void)setDateKey:(NSString *)dateKey
+{
+    _dateKey = dateKey ? [dateKey copy] : MJRefreshHeaderUpdatedTimeKey;
+}
+
 #pragma mark 设置最后的更新时间
 - (void)setUpdatedTime:(NSDate *)updatedTime
 {
     _updatedTime = updatedTime;
     
     if (updatedTime) {
-        // 1.归档
         [[NSUserDefaults standardUserDefaults] setObject:updatedTime forKey:self.dateKey];
         [[NSUserDefaults standardUserDefaults] synchronize];
-        
-        // 2.更新时间
-        // 2.1.获得年月日
+    }
+    
+    if (self.updatedTimeTitle) {
+        self.updatedTimeLabel.text = self.updatedTimeTitle(updatedTime);
+        return;
+    }
+    
+    if (updatedTime) {
+        // 1.获得年月日
         NSCalendar *calendar = [NSCalendar currentCalendar];
         NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
         NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:updatedTime];
         NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
         
-        // 2.2.格式化日期
+        // 2.格式化日期
         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
         if ([cmp1 day] == [cmp2 day]) { // 今天
             formatter.dateFormat = @"今天 HH:mm";
@@ -143,7 +153,7 @@ - (void)setUpdatedTime:(NSDate *)updatedTime
         }
         NSString *time = [formatter stringFromDate:updatedTime];
         
-        // 2.3.显示日期
+        // 3.显示日期
         self.updatedTimeLabel.text = [NSString stringWithFormat:@"最后更新:%@", time];
     } else {
         self.updatedTimeLabel.text = @"最后更新:无记录";