开发者

iOS - Assigning variable to property of an object in cellForRowAtIndexPath

I have a SQL DB that contains shops with openings days / hours per day. I calculate the day of today and want to have the property value in a custom cell.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];

switch (day) {
    case 1:
        openingsDag = @"open_ma"; 
   开发者_开发技巧     break;
    case 2:
        openingsDag = @"open_di";
        break;
    case 3:
        openingsDag = @"open_woe";
        break;
    case 4:
        openingsDag = @"open_do";
        break;
    case 5:
        openingsDag = @"open_vr";
        break;
    case 6:
        openingsDag = @"open_za";
        break;
    case 7:
        openingsDag = @"open_zo";
        break;
    default: 
        break;
}

In cellForRowAtIndexPath I assign the property values to the labels from a mutablearray:

cell.naamLabel.text = [[winkelArray objectAtIndex:indexPath.row] naam];
cell.openingsLabel.text = [[winkelArray objectAtIndex:indexPath.row] openingsDag];

This seems not to work, I have tried several other ways (stringWithFormat, cell.openingsLabel instead of cell.openingsLabel.text, ...) to get the data in the label but just can't seem to get it right.


My method to calculate the day was correct but my return type wasn't...

-(NSString*)berekenOpeningdag:(Winkel*)w {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ee"];
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];

NSString *openingsUrenReturn = @"";


switch (day) {
    case 1:
        openingsUrenReturn = w.open_ma;
        break;
    case 2:
        openingsUrenReturn = w.open_di;
        break;
    case 3:
        openingsUrenReturn = w.open_woe;
        break;
    case 4:
        openingsUrenReturn = w.open_do;
        break;
    case 5:
        openingsUrenReturn = w.open_vr;
        break;
    case 6:
        openingsUrenReturn = w.open_za;
        break;
    case 7:
        openingsUrenReturn = w.open_zo;
        break;
    default: 
        break;
}
return openingsUrenReturn;

And there, I answered my own question!


Check your date format. "dd" gives you the day of the month. Looking at your switch statement, you are expecting the day of the week. The date format string for that would be "ee".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜