在我的应用程序中,我有UITableView
3 个单元格。如果用户选择前两个单元格之一,他们可以使用 a 更改值UIDatePicker
,使其显示为例如“2011 年 11 月 11 日”。
有没有办法将第一个单元格的日期减去第二个单元格的日期?
就像用户在第一个单元格中输入“2011 年 11 月 11 日”,在第二个单元格中输入“2012 年 11 月 11 日”一样,我怎样才能让第三个单元格说“1”或“1 年”?
我的代码是 -
。H
IBOutlet UIDatePicker *datePicker;
UITableView *products;
NSMutableArray *productsInfo, *extras;
NSDateFormatter *dateFormatter;
}
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic,retain) IBOutlet UITableView *products;
@property (nonatomic,retain) NSDateFormatter *dateFormatter;;
-(IBAction)DateChanged:(id)sender;
.m -
@implementation PushedViewController
@synthesize datePicker;
@synthesize products,dateFormatter;
-(IBAction)DateChanged:(id)sender{
NSIndexPath *indexPath = [self.products indexPathForSelectedRow];
UITableViewCell *cell = [self.products cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:self.datePicker.date];
}
- (void)viewDidLoad
{
self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterLongStyle];
productsInfo = [[NSMutableArray alloc]init];
extras = [[NSMutableArray alloc]init];
[extras addObject:@"Time Left"];
[productsInfo addObject:@"Date Bought"];
[productsInfo addObject:@"Date Ending"];
//product.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"MakeText"];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section==0){
return [productsInfo count];
}
else{
return [extras count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if (indexPath.section==0){
cell.textLabel.text = [productsInfo objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];
}
else {
cell.textLabel.text = @"Time Left";
cell.detailTextLabel.text = @"8";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
}
@end