I’m creating an app that should list the date and time when a button is clicked and put the list inside a UITableView. My idea is to get the date and timestamp every time the user taps the button and then save it in an array of dictionary objects of every time and date when the button was clicked. I’ll also have another button that simply loads a modal view that displays the said UITableView with the list of the history of button clicks.
I was able to do it partially with my table getting populated with the number of dictionary entries inside the array. Problem is, it always end up with the same time and date for all of the entries.
Here’s a screenshot of the table initially with one entry.

and this what happens when I tapped the button many times. It displays the updated time for all rows.

How can I display the history in the table and prevent it from being updated? I’m simply using NSUserDefaults also in saving the data.
Here’s some code in my Button Clicked method:
- (IBAction)btnClicked:(id)sender
{
NSLog(@"Button pressed");
// Gets the current time and formats it
NSDate *timeNow = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH:mm a"];
// Gets the current date and formats it
NSDate *dateNow = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSString *currentTime = [timeFormatter stringFromDate:timeNow];
NSString *currentDate = [dateFormatter stringFromDate:dateNow];
NSString *timestamp = currentTime;
NSString *date = currentDate;
// This is where values gets saved
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:timestamp forKey:@"TimeStamp"];
[defaults setObject:date forKey:@"Date"];
[defaults synchronize];
NSString *time = [defaults objectForKey:@"TimeStamp"];
NSString *dateToday = [defaults objectForKey:@"Date"];
tableVC.tableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:time, @"Time", dateToday, @"Date", nil];
[tableVC.tableArray addObject:tableVC.tableDict];
[tableVC.table reloadData];
}
This is my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *time = [defaults objectForKey:kTimeStampText];
NSString *date = [defaults objectForKey:kDateText];
tableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:time, @"Time", date, @"Date", nil];
tableArray = [[NSMutableArray alloc] initWithObjects:tableDict, nil];
}
This is my table methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (customCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:self
options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
customCell = (CustomCell *)oneObject;
}
customCell.dateLbl.text = [tableDict objectForKey:@"Date"];
customCell.timeLbl.text = [tableDict objectForKey:@"Time"];
return customCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 74;
}
Ok I think I found your problem, you are accessing the same date for each table row.
Is incorrect
Now that should loop through it nicely and print all dates.
The problem with the code you had was that the row for index path method gets called for each row in the tableview, ie it is like a loop, so you assign each cells properties on it’s own, hope this makes sense.