In my app, I have a UITableView with 3 cells. If the user selects one of the top two cells, they can change the value using a UIDatePicker to make it say for example “November 11, 2011”.
Is there a way to subtract the dates from the first cell to the second one?
Like the user inputs “November 11, 2011” into the first cell, and in the second cell inputs “November 11, 2012”, how can I get the 3rd cell to say, “1”, or “1 year”?
My code is –
.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
In order to do this, you will have to store each of those two dates selected in the cells in instance variable to reference in the third cell. After the third cell has both dates that it needs to perform the calculation, things get a little more difficult. Using two
NSDates, you are wanting to output a time interval in human words to the user. There are not any factory methods for doing this, but a friend of mine has created a helpful class,TTTTimeIntervalFormatterthat will do just this.You can use it like so:
Then you can update the third cell using the standard
UITableViewDataSourcemethodcellForRowAtIndexPath:to update the contents of the cell.