Question

I trying to log date and time in the subtitle of the TableView I have used an array for the subtitle as I did for the text field but it didn't work for the subtitle. Am I doing something wrong? The code I have added for the subtitle pointed with *.

-(IBAction)save{

    *NSDate *now = [NSDate date];
    *NSLog(@"now: %@", now); // now: 2012-02-28 09:57:49 +0000
    *NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];


    NSUserDefaults *add1 = [NSUserDefaults standardUserDefaults];
    NSMutableArray *myList = [[[NSUserDefaults standardUserDefaults] valueForKey:@"myTxtList"] mutableCopy];
    *NSMutableArray *myDate = [[[NSUserDefaults standardUserDefaults] valueForKey:@"myDateList"] mutableCopy];

    [myList addObject:txt1.text];
    *[myDate addObject:strDate];

    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:myList] forKey:@"myTxtList"];
    *[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:myDate] forKey:@"myDateList"];

    [add1 synchronize];
    self.dataArray = myList;
    *self.dateArray = myDate;
    [self.tbl1 reloadData];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"myTxtList"];
    *self.dateArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"myDateList"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [dataArray objectAtIndex:indexPath.row];
    *NSString *dateString = [dateArray objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell==nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = string;
    *cell.detailTextLabel.text = dateString;


    return cell;
}
Was it helpful?

Solution

You're storing it as an NSDate and then reading it back as an NSString.

Also, I assume you are using ARC since you are not releasing the mutable copies of myList and myDate. If you are not on ARC, you should be releasing these or else it will leak.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top