0

在我的应用程序中,我有UITableView3 个单元格。如果用户选择前两个单元格之一,他们可以使用 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
4

2 回答 2

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)
    }
}       

然后用你的细胞做你想做的事。

于 2011-11-11T19:21:12.933 回答
0

为此,您必须将在单元格中选择的这两个日期中的每一个都存储在实例变量中,以便在第三个单元格中引用。在第三个单元格具有执行计算所需的两个日期之后,事情变得更加困难。使用两个NSDates,您希望以人类语言向用户输出时间间隔。没有任何工厂方法可以做到这一点,但我的一个朋友创建了一个有用的类,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:以更新单元格的内容。

于 2011-11-11T19:30:54.257 回答