在 UITableView 中添加/删除单元格
在我的应用程序中,我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您必须将在单元格中选择的两个日期中的每一个存储在实例变量中以在第三个单元格中引用。当第三个单元格具有执行计算所需的两个日期后,事情变得有点困难。使用两个 NSDate ,您希望向用户输出人类语言的时间间隔。没有任何工厂方法可以执行此操作,但我的一个朋友创建了一个有用的类,
TTTTimeIntervalFormatter
就可以做到这一点。
您可以像这样使用它:
然后您可以使用标准
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
NSDate
s, 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:
Then you can update the third cell using the standard
UITableViewDataSource
methodcellForRowAtIndexPath:
to update the contents of the cell.好吧,你可以这样做:
然后用你的手机做你想做的事。
Well, you can do it in the:
And then do what you want with your cell.