在我的应用程序中,我有一个带有3个单元格的UITableView
。如果用户选择最上面的两个单元格中的一个,则他们可以使用UIDatePicker
来改变该值,以例如说明“2011年11月11日”。在UITableView中添加/减去单元格
有没有办法从第一个单元格到第二个单元格中减去日期?
像用户输入“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
这正是我想要做的,但事情是,我没有2 UIDatePickers,只有1.我会更新我的代码来显示你,它是在原来的职位。 顺便说一句,感谢您的帮助 –
好吧,这并没有真正改变我的答案。您只需在用户从选取器中选择一个日期后根据所选单元格存储日期。如果只使用单个日期选择器,则无关紧要 - 只需将日期存储在适当的实例变量中,以便在有两个日期时可以在第三个单元格上进行计算。 – larsacus
我该如何声明NSDates与行中的单元格? –