1

I'm using the below code to retrieve a series of 7 dates, and add them to an NSMutableArray.

That said, I want to display each date stored inside the array in each cell's UILabel (see below). How can I write that UILabel line so that it pulls the date out of weekdates?

Dashboard.h

@property (weak, nonatomic) IBOutlet UITableView *todaytableView;
@property (strong, nonatomic) NSMutableArray *weekdates;

DashboardViewController.m

 - (void)viewDidLoad {
    [super viewDidLoad];

      _weekdates = [[NSMutableArray alloc] init]; //Add this line

       NSDate *today = [NSDate date];
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
       NSCalendar *calendar = [NSCalendar currentCalendar];
       dateFormatter.dateFormat = @"ddMMM";
       for (UInt16 i = 0; i < 7; i++) {
           NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:today options: 0];
           [_weekdates addObject:nextDate];
           NSLog(@"Weekdate %d = %@", i+1, [dateFormatter stringFromDate: nextDate]);
           
       }

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
    return 6;
    
}


-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    if (tableView == self.todaytableView) {
        
              
    
    static NSString *WeeklyTableIdentifier = @"TimeTableViewCell";
    
    TimeTableViewCell *cell = (TimeTableViewCell *)[self.todaytableView dequeueReusableCellWithIdentifier:WeeklyTableIdentifier];
    
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimeTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        
    }
    
     
        cell.firstLast.text = [NSString stringWithFormat:@"%@", _weekdates[indexPath.row]];
       
        
        
    return cell;
    
}
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • 1
    I suggest creating all your dates outside of the tableview support methods and putting them in an array. Then when you want one for the table, get it by using indexPath.row on the array. – Phillip Mills Sep 30 '20 at 23:25
  • `cell.firstLast.text = weekdates[@"date"];`, that should be `cell.firstLast.text = [NSString stringWithFormat:@"%@", weekdates[@"date"]];` minimum, that's why it fails. It's expecting a `NSString` and you give its a `NSDate`. But, the rest is: use a `NSDateFormatter` for `NSDate` to `NSString`, The way you do the loop is wrong, it's not to be done here. That's a misunderstanding on how works a `UITableView`. – Larme Oct 01 '20 at 09:02
  • @Larme Took your advice and went with the NSMutableArray - works! See my revised code above. That said, when I display it in the UILabel, it doesn't keep my dateformatter format? E.g. it's displayed in my label as: 2020-10-01 22:58:38 +0000 – Brittany Oct 01 '20 at 23:01
  • Look at where the dateFormatter is being used – it's only going into the NSLog statement. Your `[weekDates addObject...]` doesn't use the formatter so the objects in the array do not have any date format applied. They are dates, not strings. (Easy to miss this error because both `stringWithFormat:` and `addObject:` can accept any type of object. If you were to use [generics](https://microsoft.github.io/objc-guide/Syntax/Generics.html), or just Swift, that might help address this confusion.) Either use the date formatter in your cellForRowAtIndexPath or store the formatted strings in the array. – jtbandes Oct 01 '20 at 23:12
  • Ahh duh. Lol thank you @jtbandes! – Brittany Oct 01 '20 at 23:17
  • Note that you're creating 7 dates and telling the table it has 6. – Phillip Mills Oct 02 '20 at 00:16

2 Answers2

0

In _weekdates you stored the date so when you want to show in label first convert to string and display. put this code in cellForRowAtIndexPath

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"ddMMM";
NSDate *nextDate = _weekdates[indexPath.row];
NSString *strDate = [dateFormatter stringFromDate:nextDate];
cell.firstLast.text = strDate;
Dhawal
  • 1,055
  • 6
  • 10
-2

Looks like your for loop is incorrect syntactically.

for (NSInteger i = 0; i < 7; ++i)

has "++i" when it should have "i++".

for (NSInteger i = 0; i < 7; i++)
JAH-FH
  • 119
  • 6
  • 1
    That code is perfectly valid (and it works the same either way), see https://stackoverflow.com/questions/484462/difference-between-pre-increment-and-post-increment-in-a-loop – jtbandes Oct 01 '20 at 23:09