1

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;
}
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51

1 Answers1

2

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.

Joel
  • 15,654
  • 5
  • 37
  • 60
  • Thank you Joel. I have edited the code, I have changed it as string but it is still not working. `myList` is working without `myDate`. Normally I am using iOS5 so I don't want to use ARC. How could I solve my problem? Thanks again Joel. – Luai Kalkatawi May 10 '12 at 07:02
  • 1
    In your cellForRowAtIndexPath method, log the count of dateArray, the indexPath.row and then loop through the whole array and log each entry in the array. Then you can see if the contents of the array are correct when you are trying to read it. You have to determine if your issue is the array itself or trying to set the subtitle. Once you locate the issue, it will be easy to solve. – Joel May 10 '12 at 07:51
  • Thanks Joel. After I added `return [dateArray count];` in the `numberOfRowsInSection` it works. I thought I can't add two count in the `numberOfRowsInSection`. Thanks again. – Luai Kalkatawi May 10 '12 at 11:56