在 UITableView 中添加/删除单元格

发布于 2024-12-15 07:24:39 字数 2961 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-12-22 07:24:39

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

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, TTTTimeIntervalFormatter that will do just this.

You can use it like so:

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];

Then you can update the third cell using the standard UITableViewDataSource method cellForRowAtIndexPath: to update the contents of the cell.

酒几许 2024-12-22 07:24:39

好吧,你可以这样做:

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

然后用你的手机做你想做的事。

Well, you can do it in the:

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

And then do what you want with your cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文