Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:17:12+00:00 2026-06-06T07:17:12+00:00

I’m creating an app that should list the date and time when a button

  • 0

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.
enter image description here

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

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:17:14+00:00Added an answer on June 6, 2026 at 7:17 am

    Ok I think I found your problem, you are accessing the same date for each table row.

    customCell.dateLbl.text = [tableDict objectForKey:@"Date"];
    customCell.timeLbl.text = [tableDict objectForKey:@"Time"];
    

    Is incorrect

    NSDictionarry *dict = [tableArray objectAtIndex:[indexPath row]];
    customCell.dateLbl.text = [dict objectForKey:@"Date"];
    customCell.timeLbl.text = [dict objectForKey:@"Time"];
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
i want to parse a xhtml file and display in UITableView. what is the
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.