2015-10-18 69 views
0

我的应用有一个UITableView,它有12行,其行的单元格文本和高度根据时间以小时为单位进行设置,即当时钟位于6时,只在UITableview的第6行显示文本,只增加第6行的大小,保留其余行的文本隐藏,行高减小。如何在更改时间(小时)时更新我的​​应用UI

我使用NSDateComponents获取当前时间的小时数。问题是应用程序第一次加载时,行位置显示正确。 当应用程序的操作数和时间改变时,UI不会更新,即行位置不会更改。 我想我需要NSNotificationCentre来通知何时发生更改,然后用它更新行位置。

任何人都可以解释我可以做到这一点吗?

这里是我的应用程序代码。

TimeInfo.h

#import <Foundation/Foundation.h> 
@interface TimeInfo : NSObject 

@property (nonatomic) NSInteger timeNow; 
-(NSInteger)currentTimeInHour; 

TimeInfo.m

#import "TimeInfo.mh" 

@implementation TimeInfo 

-(NSInteger)currentTimeInHour{ 
    NSDate *now = [NSDate date]; 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [calendar components:NSCalendarUnitHour fromDate:now]; 
    NSInteger hour = [components hour]; 
    return hour; 
} 

@end 

tableViewController.h

#import <UIKit/UIKit.h> 
#import "TimeInfo.h" 

@interface TableViewController : UITableViewController 


@property (nonatomic) NSInteger timeInHour; 

@end 

tableViewController.m

#import "TableViewController.h" 

@interface TableViewController() 
{ 
    NSMutableArray *someArray; 
} 

@end 

@implementation TableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    TimeInfo *first = [[TimeInfo alloc]init]; 
    self.timeInHour = [first currentTimeInHour]; //call method to get time in hour 

    someArray = [[NSMutableArray alloc]init]; 
    [someArray insertObject:@"19" atIndex:0 ]; 
    [someArray insertObject:@"20" atIndex:1 ]; 
    ........................................... 
    [someArray insertObject:@"45" atIndex:12]; // this is just for example, though I am loading data in array from plist. //not shown here. 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return somerArray.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 


    if (self.timeInHour <= 12) { 
     NSInteger firstHalf = self.timeInHour - 1; 
     if (indexPath.row == firstHalf) { 
      cell.textLabel.text = someArray[firstHalf] 
     } 

    } else if (self.timeInHour > 12){ 
     NSInteger secondHalf = self.timeInHour -13; 
     if (indexPath.row == secondHalf) { 
      cell.textLabel.text = someArray[secondHalf]; 
     } 
    } 
     return cell; 
} 


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    __block int blockValue = 45; //this is height for all the row except one which will be set from inside block and will match the cellForRowAtIndex method also// value will get changed from inside block. 

    void (^tableRowHeightForThisHour)(void) = ^{ 

     if (self.timeInHour <= 12) { 
      NSInteger firstHalf = self.timeInHour - 1; 
      if (indexPath.row == firstHalf) { 
       blockValue = 172; 
      } 

     } else if (self.timeInHour > 12){ 
      NSInteger secondHalf = self.timeInHour -13; 
      if (indexPath.row == secondHalf) { 
       blockValue = 172; 
     } 
    } 

    }; //block ends here. 

    tableRowHeightForThisHour(); 
    return blockValue; 
} 
+0

与其说这是在viewDidLoad中的,在ViewDidAppear调用它,并呼吁重新加载的tableView。 –

+0

结果相同,因为此代码仅在首次启动时检查时间。如果应用程序持续存在并且时间在两者之间变化,则变更未被传达。我需要一些方法来通知时间已经改变,所以有时间来相应地更新表视图行的位置。 – Alok

回答

1

我会处理这个问题的方法如下:

  1. 与类别替换上NSDateTimeInfo类 - 你想要的功能是作为一种最好的思想扩展到NSDate。喜欢的东西:

    @interface NSDate (currentHourInDay) 
    -(NSInteger)currentHourInDay; 
    @end 
    

    (h文件)

    #import "NSObject+currentHourInDay.h" 
    
    @implementation NSDate (currentHourInDay) 
    
    -(NSInteger)currentHourInDay { 
        NSCalendar *calendar = [NSCalendar currentCalendar]; 
        NSDateComponents *components = [calendar components:NSCalendarUnitHour fromDate:self]; 
        NSInteger hour = [components hour]; 
        return hour; 
    } 
    
    @end 
    

    (.m文件)

    这是基本相同的方法,但改名为使其更清晰略微它做什么。 (currentTimeInHour可能意味着'小时数/分钟数'以及'小时数'的当前时间)。显然你需要将这个导入你的VC来使用它。

    你也应该将表视图控制器上更改属性的名称hourInDay

  2. 创建一个NSTimer,火每分钟(或长不过你要检查之间的间隔)。理想情况下,把它放在一个物业。

    @property (nonatomic,strong) NSTimer* timer; 
    

    用一些适当的值启动它。请记住,启动计时器会在保留计时器对象的runloop上进行调度。这意味着即使发生调度的对象被释放,它也会继续触发。如果您需要在对象解除分配或不再使用时停止它,则可以在某个适当的位置使用[_timer invalidate]来完成此操作。 deallocviewWillDisappear: - 这就是为什么你需要一个属性。

    NSTimeInterval minuteInSecs = 60.0; 
    _timer = [NSTimer scheduledTimerWithTimeInterval:minuteInSecs target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; 
    

    请注意,它是timerFired:NOT timerFired。如果你错过了冒号,它将无法工作 - 这意味着选择器需要一个参数。

  3. 在同一个对象上实现定时器的回调。其中,只需检查小时是否已更改,如果已更改,则更新hourInDay并重新加载表格。如果没有,则什么也不做。

    -(void)timerFired:(NSTimer*)timer { 
        int newHourInDay = [[NSDate date] currentHourInDay]; 
        if(newHourInDay != self.hourInDay) { 
        self.hourInDay = newHourInDay; 
        [self.tableView reloadData]; 
        } 
    } 
    

您现有的逻辑应该处理剩下的

+0

我怎么忘了NSTimer。这确实奏效,感谢这一广泛的解释。同意创建类别。你认为这可以通过使用NSNotificationCentre来完成。 – Alok

相关问题