views:

155

answers:

2

I have attempted this is many ways but failed consistently, hopefully you guys can help me achieve the what i want to do.

I am making an iPad app, I will have five tables in a single view and each table will have a date as a header/title in this format, e.g. Monday 20

These five tables will be monday to friday. This is the bit I cant do. I want to work out the current date and then highlight the table which is today, obviously changes everyday.

So for example, lets say today is Thursday 9th. Thursday table is highlighted and then is automatically sets the date of the other tables around thursday.

Think of a school timetable/planner/diary. Monday to friday, each labelled with their dates.

EDIT: So what if I did it like this? If I add this into the code you gave me, if TRUE (button pressed) add seven days, done just like in the example form Apple. However my issue, what is gregorian? what do I replace it with? I have seen it used lots on calendar samples from Apple.

if (tableView == monTable){
        if(next == TRUE){
            [comps setDay:7];
            NSDate *date = [gregorian dateByAddingComponents:comps toDate:curDate  options:0];
        }
        else{
            [comps setWeekday:2];
        }
    }
A: 

You can get Monday to Friday dates this way:

NSDate* curDate = [NSDate date]; // Get current date
NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
for (int i = 2; i <= 6; i++){ 
    [comps setWeekday:i];
    NSDate *tDate = [calendar dateFromComponents:comps];
    NSLog(@"%@", tDate);
}

To determine which date to highlight (current date) you just need to check date's weekday component.

Edit: titleForHeaderInSection method may look like:

- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section{
    NSDate* curDate = [NSDate date]; // Get current date
    NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
    NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
    if (tableView == monTable)
        [comps setWeekday:2];
    if (tableView == tueTable)
        [comps setWeekday:3];
    if (tableView == wedTable)
        [comps setWeekday:4];
    if (tableView == thuTable)
        [comps setWeekday:5];
    if (tableView == friTable)
        [comps setWeekday:6];

    NSDate *tDate = [calendar dateFromComponents:comps];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"EEE, MMM d"];


    return [formatter stringFromDate:tDate];
}
Vladimir
Ahh ok, but if I am using the code you provided in titleForHeaderInSection, how do I get it to go through each table and give it the correct date?For example, I have monTable, tueTable, wedTable etc. But how do I get it to assign the correct date to the correct table?
Josh Kahane
for example you can set tag property to your tables (say, from 1 to five) and set weekday like this: [comps setWeekday:1 + table.tag];, or just 'it (table == monTable) [comps setWeekday:2]; etc for each table.
Vladimir
Hmm, thanks for help, it all makes sense, but my app crashes when I add these if statements. I put your code inside the titleForHeaderInSection and added these of statements into the for loop.f(tableView == monTable) [comps setWeekday:2]; if(tableView == tueTable) [comps setWeekday:3]; if(tableView == wedTable) [comps setWeekday:4]; if(tableView == thuTable) [comps setWeekday:5]; if(tableView == friTable) [comps setWeekday:6];Also, would I not need to 'return' these if statements? If I try to do that, it throws up an error.
Josh Kahane
What error do you get with crash? And you don't need for loop in this case - just put your if statements instead of it
Vladimir
In those if-statements you just need to set appropriate weekday component value. after that (that is after all if-statements)- create nsdate object from comps and yes you'll need to format the date to an appropriate string value (using NSDateFormatter) and return that string.
Vladimir
Hmm, no success, could you give an example please? I unfortunately don't quite have enough experience with NSDateComponents. Not quite sure what to try.Thanks for the help so far, really appreciated.
Josh Kahane
My goodness it works wonderfully! Thank you so much, now I don't want to over ask for help, but on the same topic, linked with this, ho can I adjust these dates plus or minus 7 days? so i can look through the weeks.
Josh Kahane
change 'week' property in comps :)
Vladimir
My issue is though I can't do this with an IBAction as its declared inside titleForHeaderInSection. I have tried adjusting this, still unsuccessful, sorry you will have to be patient with me. :)Or more so my issue, what numbers can I use with setWeek: ? Naturally for weekDay its 1-7, but Im not so sure here even after checking the documentation.
Josh Kahane
Have a look at rangeOfUnit:inUnit:forDate: method in NSCalendar class - you can get the range of weeks in month for current date with it
Vladimir
Making excellent progress and learnt loads thanks Vladimir! One last bit of help though please! I have made some good progress on altering the week, so adding a week or going to the previous week. Using [comps setWeek: ] or an appropriate alternative method, how can i do this? I have got the button linked up nicely and a better understanding of how it works now but can't quite get it going.
Josh Kahane
I rechecked docs - better way to change current week may be to use dateByAddingComponents:toDate:options: method of NSCalendar - have a look at it in reference (there's example there on how to use that method)
Vladimir
Ahh this makes a lot of sense! Seems like a simple implementation of the code you provided. I edited my original post, check it out please.Ahh dont worry I understand, it was a calendar, declared as 'calendar' in your sample. Hmm, can't get it working.
Josh Kahane
Dont worry its working now! I used setHour:168 (seven days) instead of setDay and it works! Thanks so much.
Josh Kahane
A: 

Also see:

http://github.com/billymeltdown/nsdate-helper

ceeit