2011-11-11 161 views
0

在我的应用程序中,我有一个带有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 

回答

0

为了做到这一点,您必须将实例变量中单元格中所选的两个日期中的每一个都存储在第三个单元格中。在第三个单元有两个需要执行计算的日期之后,事情会变得更加困难。使用两个NSDate s,您希望向用户输出人类单词的时间间隔。没有任何工厂方法可以做到这一点,但我的一位朋友创建了一个有用的课程,TTTTimeIntervalFormatter即可做到这一点。

您可以使用它像这样:

NSDate *dateFromFirstDatePicker = [datePicker1 date]; 
NSDate *dateFromSecondDatePicker = [datePicker2 date]; 
TTTTimeIntervalFormatter *timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init]; 

NSString *timeInterval = [timeIntervalFormatter stringForTimeInterval:[dateFromFirstDatePicker timeIntervalSinceDate:dateFromSecondDatePicker]]; 

//other examples 
[timeIntervalFormatter stringForTimeInterval:0]; // "just now" 
[timeIntervalFormatter stringForTimeInterval:100]; // "1 minute ago" 
[timeIntervalFormatter stringForTimeInterval:8000]; // "2 hours ago" 

[timeIntervalFormatter release]; 

然后你可以使用标准UITableViewDataSource方法cellForRowAtIndexPath:更新单元格的内容更新的第三个单元格。

+0

这正是我想要做的,但事情是,我没有2 UIDatePickers,只有1.我会更新我的代码来显示你,它是在原来的职位。 顺便说一句,感谢您的帮助 –

+0

好吧,这并没有真正改变我的答案。您只需在用户从选取器中选择一个日期后根据所选单元格存储日期。如果只使用单个日期选择器,则无关紧要 - 只需将日期存储在适当的实例变量中,以便在有两个日期时可以在第三个单元格上进行计算。 – larsacus

+0

我该如何声明NSDates与行中的单元格? –

0

嗯,你能做到这一点的:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if([indexPath row] == 1){ 
     // Get the date from the cell: 
     UITableViewCell *cell=[table cellForRowAtIndexPath:indexPath]; 
     // Take care of the date 
     // (...) 
     // Give it to the other cells: 
     UITableViewCell *cell=[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]; // this is a reference to the second cell in the table (I assume you have 3: 0,1,2) 
    } 
}  

,然后做你想要的细胞。

+0

我看到你在说什么,但我怎么能减去细胞,并将该日期放入第三个单元格,同时保持相同的日期格式? –

相关问题