Add one row to TableView each day app used

六眼飞鱼酱① 提交于 2019-12-06 02:33:43

I would store a date value into your NSUserDefaults and use it to compare and see if the day has changed, then use that to modify the number of rows in the section. I have heavily commented this code, hoping it will better explain.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    int numberOfDays = 0;   //initialize integer variable    

    //get the current date from the user's device
    NSDate *now = [NSDate date];

    //create a dateformatter to handle string conversion
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    // String to store in defaults.
    NSString *todaysDateString = [dateFormatter stringFromDate:now];

    // get access to the user defaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults valueForKey:@"date_last_opened"]) {

        // if there is no value for the key, save today's date as the string we formatted
        [defaults setValue:todaysDateString forKey:@"date_last_opened"];

    } else {

        // there is already a value for date-last-opened, so pull it from the defaults, and convert it from a string back into a date.
        NSDate *dateLastOpened = [dateFormatter dateFromString:[defaults valueForKey:@"date_last_opened"]];

        if ([dateLastOpened compare:now] == NSOrderedAscending) {

            // if the date_last_opened is before todays date, get the number of days difference.

            unsigned int unitFlags = NSDayCalendarUnit;
            NSDateComponents *comps = [calendar components:unitFlags fromDate:dateLastOpened  toDate:now options:0];

            numberOfDays = [defaults integerForKey:@"totalDays"] + [comps day];
        [defaults setInteger:numberOfDays forKey:@"totalDays"];
        }
    }

return numberOfDays;

}

EDIT: This code assumes, you've already set an NSUserDefault value for a key similar to @"totalDays" to 1 when the app first launches, like in viewDidLoad maybe:

if (![defaults integerForKey:@"totalDays"]) {

        // if there is no value for the key, set it to 1
        [defaults setInteger:1 forKey:@"totalDays"];

    }

In your class AppDelegate.m you can do this :

//Application did launch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

//The application was in background and become active
- (void)applicationWillEnterForeground:(UIApplication *)application
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

Then using the NSUserDefault Key @"LaunchCount" you can add an Table-Row.

Sounds like you want one section with [array count] rows, whereas right now you're returning [array count] sections AND [array count] rows.

How about something like a NSTimeInterval of 24 hours.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!