我的应用有一个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;
}
与其说这是在viewDidLoad中的,在ViewDidAppear调用它,并呼吁重新加载的tableView。 –
结果相同,因为此代码仅在首次启动时检查时间。如果应用程序持续存在并且时间在两者之间变化,则变更未被传达。我需要一些方法来通知时间已经改变,所以有时间来相应地更新表视图行的位置。 – Alok