0

我正在开发一个教育任务管理器,我想在单元格的字幕部分添加一个时间戳。我已经正确编码了任务的标题,但我想用创建任务时的时间戳替换副标题。

在此处输入图像描述

我想把它说字幕的地方变成时间戳

有人可以帮我吗?只需在下面评论我需要添加到附加到此 TableView 的控制器的代码。下面是我的 ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource>

-(IBAction)add:(UIBarButtonItem *)sender;
@property (nonatomic, strong) NSMutableArray *tasksArray;
@property (nonatomic, strong) IBOutlet UITableView *tasksTable;

@end

这是我的 ViewController.m

@interface ViewController ()

@end

@implementation ViewController

@synthesize tasksArray = _tasksArray;
@synthesize tasksTable = _tasksTable;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"Test");
    self.tasksArray = [[NSMutableArray alloc]init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)add:(UIBarButtonItem *)sender
{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Enter the task name"          message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTile = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTile isEqualToString:@"Add"]) {
        [self.tasksArray addObject:[alertView textFieldAtIndex:0].text];
        [self.tasksTable reloadData];
    }
}

#pragma mark
#pragma table view

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tasksArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [self.tasksArray objectAtIndex:indexPath.row];

    return cell;
}

@end

我是 StackOverFlow 的新手,所以如果我的帖子很糟糕,请不要评判:p 哈哈!干杯,请帮助我!

4

1 回答 1

2

要创建数据数组:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTile = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTile isEqualToString:@"Add"]) {    
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

        [dictionary setObject:[alertView textFieldAtIndex:0].text forKey:@"text"];
        [dictionary setObject:[NSDate date] forKey:@"date"];

        [self.taskArray addObject:dictionary];

        [self.tasksTable reloadData];
    }
}

在你的 cellForRow

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
    }

    NSDictionary *currentDictionary = [self.taskArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [currentDictionary objectForKey:@"text"];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];

    NSString *dateString = [dateFormat stringFromDate:[currentDictionary objectForKey:@"date"]];
    cell.detailTextLabel.text = dateString;

    return cell;
}

您可以在此处查看日期格式化程序值

于 2013-06-12T21:37:57.457 回答