Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

HELP: Label to show/hide on certain day of the week

DarntonDarnton LondonPosts: 5New Users Noob
edited September 2015 in iOS SDK Development
Hey guys, I'm a nooby... but started to understand little bits of what I'm doing as I'm going along.

Basically I want mondayBoldLabel to show when it is Monday and hide the mondayLabel when it is not. What I have put together below is pieced together from lots of different forums/videos online, but doesn't show any errors.

Any help would be much appreciated.

Thanks

Lewis

ExpandingCell.h
[i]#import <UIKit/UIKit.h>

@interface ExpandingCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *subtitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *mondayTimesLabel;
@property (strong, nonatomic) IBOutlet UILabel *tuesdayTimesLabel;
@property (strong, nonatomic) IBOutlet UILabel *wednesdayTimesLabel;
@property (strong, nonatomic) IBOutlet UILabel *thursdayTimesLabel;
@property (strong, nonatomic) IBOutlet UILabel *fridayTimesLabel;
@property (strong, nonatomic) IBOutlet UILabel *saturdayTimesLabel;
@property (strong, nonatomic) IBOutlet UILabel *sundayTimesLabel;

@property (weak, nonatomic) IBOutlet UILabel *mondayLabel;
@property (weak, nonatomic) IBOutlet UILabel *mondayBoldLabel;

@end[/i]

ExpandingCell.m
[i]#import "ExpandingCell.h"

@implementation ExpandingCell
@synthesize titleLabel, subtitleLabel, mondayTimesLabel, tuesdayTimesLabel, wednesdayTimesLabel, thursdayTimesLabel, fridayTimesLabel, saturdayTimesLabel, sundayTimesLabel, mondayBoldLabel, mondayLabel;

-(void)showtext
{
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"EEEE"];
    NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);
    if(  [self isItMonday])
    {
        [self.mondayBoldLabel setHidden:NO];
        [self.mondayLabel setHidden:YES];
        [self.mondayBoldLabel setText:[dateFormatter stringFromDate:[NSDate date]]];
        
    }
    else
    {
        [self.mondayBoldLabel setHidden:YES];
        [self.mondayLabel setHidden:NO];
        [self.mondayLabel setText:[dateFormatter stringFromDate:[NSDate date]]];
        
    }
}

-(BOOL)isItMonday
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
    NSInteger currentWeekday = [components weekday];
    
    if (currentWeekday == 1 ) {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end[/i]
Images attached are the ExpandingCell.xib
Post edited by Darnton on

Replies

  • DarntonDarnton LondonPosts: 5New Users Noob
    edited September 2015
    What I have done probably makes no sense and I apologise for any frustration caused.
    Post edited by Darnton on
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    1. Use code tags

    2. File attachments don't actually work here. You'll need to host the images elsewhere and link them manually.

    3. Do you truly need a second label to handle this situation? Why not just change the font for a single label?

    4. I'm not entirely clear what you are showing with the screen shots. Is the one with 2 labels what you are actually seeing, and the one with only 1 label what you would prefer to see instead? Your problem in general is not clearly stated.

    5. FYI, you generally don't need the @synthesize lines anymore. It's handled automatically.

    6. Just curious, what is the reason you are using a date formatter for the label text?
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • RickSDKRickSDK Posts: 1,240Registered Users @ @ @ @
    I'm confused. is your code working or not working. if its not working is it getting errors or just not working as expected. if not as expected, what is it doing instead?

    After studying the code for a while, it appears you may be using two separate labels just because you sometimes want the text bold and other times want it not bold. Why not just use code to make it bold?
  • DarntonDarnton LondonPosts: 5New Users Noob
    edited September 2015
    1. Done.

    2. Noted.

    3. Probably don't need another label no, but it was what I found how to 'do' and thought it could be a possible solution.

    4. I was just showing the context of the label, (have now deleted the other image) - basically on a Monday I want 'Monday' to be displayed and on other days (non-mondays) I want the regular format 'Monday' to be displayed. My problem is that using the code above - both labels are visible, when it should be only one dependant on the weekday.

    5. Noted.

    6. I'm very fresh to this, I only started yesterday ^ explanation in 4.

    Thanks for your reply

    Lewis
    Post edited by Darnton on
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Yeah, just conditionally change font. There's no reason to have 2 labels for this. Or 14, assuming that you're eventually going to do this for each day.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • DarntonDarnton LondonPosts: 5New Users Noob
    Hi Rick, sorry for the confusion.

    The code works, no errors, but isn't doing the desired action.

    Instead it is just showing both labels rather than hiding one and displaying the other.

    Thank for you taking the time to study the code :smile: yes that is a more viable solution, however I don't know how to do this, I'll have a look into it.

    Lewis
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    More or less along these lines:
    UIFont *normalFont = [UIFont systemFontOfSize:12.0];
    UIFont *boldFont = [UIFont boldSystemFontOfSize:12.0];
    
    [mondayLabel setFont: ([self isItMonday]) ? boldFont : normalFont];
    [tuesdayLabel setFont: ([self isItTuesday]) ? boldFont : normalFont];
    ...
    

    There might be a cleverer way of doing it, but this will suffice.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • DarntonDarnton LondonPosts: 5New Users Noob
    Excellent Brian, I don't really know how to implement it, but will have a play around see what I can do.

    Cheers

    Lewis
Sign In or Register to comment.