【问题标题】:Add/Subtract cells in UITableView在 UITableView 中添加/减去单元格
【发布时间】:2011-12-27 04:46:59
【问题描述】:

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

【问题讨论】:

    标签: ios xcode uitableview cells subtraction


    【解决方案1】:

    为此,您必须将单元格中选定的两个日期分别存储在实例变量中,以便在第三个单元格中引用。在第三个单元格具有执行计算所需的两个日期之后,事情变得更加困难。使用两个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:更新第三个单元格的内容了。

    【讨论】:

    • 这正是我想要做的,但问题是我没有 2 个 UIDatePickers,只有 1 个。我会更新我的代码给你看,它在原始帖子中。顺便说一句,感谢您的所有帮助
    • 好吧,这并没有真正改变我的答案。您只需要在用户根据所选单元格从选择器中选择日期后存储日期。是否只使用一个日期选择器并不重要 - 只需将日期存储在适当的实例变量中,这样当您有两个日期时,您就可以在第三个单元格上进行计算。
    • 如何在行中的单元格中相应地声明 NSDates?
    • 当用户点击一行时,让它显示一个日期选择器。当用户完成输入日期后,使用[UIDatePicker date]UIDatePicker 中的NSDate 存储在名为date1 的实例变量中。在第二次约会时做同样的事情。存储两个日期后,只需使用上面的代码调用第三个单元格即可获取时间间隔。
    【解决方案2】:

    嗯,你可以在:

    -(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-10-17
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多