我的应用程序有一个 12 行的 UITableView,其单元格文本和行高是根据以小时为单位的时间设置的,即当时钟为 6 时,只在 UITableview 的第 6 行显示文本并增加第 6 行的大小只是,隐藏其余的行文本并缩小它们的行高。
我使用 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;
}
最佳答案
我的处理方式如下:
将
TimeInfo
类替换为NSDate
上的类别 - 最好将您想要的功能视为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 中才能使用。
您还应该将 TableView Controller 上的属性名称更改为
hourInDay
创建一个
NSTimer
,每分钟触发一次(或者无论您希望检查之间的间隔有多长)。理想情况下,将其放在属性中。@property (nonatomic,strong) NSTimer* timer;
使用一些适当的值启动它。请记住,启动计时器会在保留计时器对象的运行循环中进行调度。这意味着即使调度发生的对象被释放,它也会继续触发。如果您需要在对象解除分配或不再使用时停止它,您可以在适当的地方使用
[_timer invalidate]
来做到这一点,例如dealloc
或viewWillDisappear:
- 这就是您需要属性的原因。NSTimeInterval minuteInSecs = 60.0; _timer = [NSTimer scheduledTimerWithTimeInterval:minuteInSecs target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
请注意,它是 timerFired:不是 timerFired。如果您漏掉了冒号,它将无法工作 - 这意味着选择器需要一个参数。
在同一个对象上实现定时器的回调。在其中,只需检查小时是否已更改,如果已更改,则更新
hourInDay
并重新加载表。如果没有,则什么也不做。-(void)timerFired:(NSTimer*)timer { int newHourInDay = [[NSDate date] currentHourInDay]; if(newHourInDay != self.hourInDay) { self.hourInDay = newHourInDay; [self.tableView reloadData]; } }
您现有的逻辑应该处理其余部分
关于ios - 如何在时间(以小时为单位)更改时更新我的应用程序用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198377/