UA-17470720-3

Jump to content


Photo
- - - - -

Segment Control - Yes / No - Show / Hide Image


  • Please log in to reply
7 replies to this topic

#1 designwerks

designwerks

    Advanced Member

  • STV 5.0 Std
  • PipPipPipPipPip
  • 141 posts
  • LocationKingsville, Ontario, Canada
Reputation: 23
Excellent

Posted 27 October 2015 - 11:15 AM

Folks,

 

Trying to think of a way to have the user place a little heart in their TableViewCell if the information is a favourite item or not.

 

Basically, select from a segment control yes / no - have that working in Core Data. Since the Core Data is saved as a string, not sure how to do this.

 

I have set up a UIImageView with the image in it, and basically want to use the hidden = no or hidden = yes.  I have something like this but it is not working...

 

if(indexPath.section == 0) {

        

        BOOL test = [[cell.boundObject valueForKey:@"favorite"] integerValue];

        NSLog(@"%@", (test ? @"YES" : @"NO"));

        

        if(test == YES) {

            self.favoriteImage.hidden = NO;

        }

        else {

            self.favoriteImage.hidden = YES;

        }

 

I have this code in my willConfigure... No error message - just that the code simply does not display anything.  Has anyone successfully coded something like this?

 

John



#2 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 27 October 2015 - 04:12 PM

 Since the Core Data is saved as a string

 

what part are you not sure about with this statement as im not able to guess what is being saved?

is the db property your talking about "favorite"??

 

so is the property 'favorite' a integer or bool?? coz you have it setting a bool TEST using an integerValue.. if favorite is a bool then use boolForKey to set test..

 

what does you NSLog write out? does this code even get accessed??

if test is an integer then the if should be if(test == @YES)

 

"the code simply does not display anything" but does the code execute at all?

does self.fovoriteImage have a image in there already so it can display something

 

lastly :) you should not be using SELF inside block code:) should be using a weak self object of self


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#3 designwerks

designwerks

    Advanced Member

  • STV 5.0 Std
  • PipPipPipPipPip
  • 141 posts
  • LocationKingsville, Ontario, Canada
Reputation: 23
Excellent

Posted 27 October 2015 - 05:12 PM

Trying a completely different approach since the "Favourites" is a segmented controller using Yes / No as a NSString... Here is my code so far... it works but repeats itself over the cells like every 6th cell copies what ever the first 5 tableview cells look like... 

 

The code looks for the Yes or No from the segment controller saved as valueForKey favorite... if "Yes", it displays the heart symbol if not no image is displayed...

My problem now is the repeat... below 5 cells... say I have 20 cells created... the first 5 cells dictate what happens to the other cells regardless of their setting...

 

// show heart symbol on favorite plant by user selecting Yes / No from Segmented Controller

        

        NSString *yesText = @"Yes";

        

        if ([[cell.boundObject valueForKey:@"favorite"] isEqualToString:yesText]) {

            

            [favoriteImage setImage:[UIImage imageNamed:@"heart"]];

            NSLog(@"Yes - Favorite");

            

        }

        else

        {

            [favoriteImage setImage:nil];

            NSLog(@"No - Not a Favorite");

            

        }

        

        // end of favorite plant code

 

 

 

Not bad but the item wants to repeat itself...



#4 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 27 October 2015 - 09:57 PM

Greetings Program!

 

Assuming it was in the willDisplay action in PlantDetail.m, I replicated the issue by adding favoriteImage as an IBOutlet property in the class (which you don't want to do). Is this how you have it?

 

You need to access the UIImageView in the cell so give it a tag and do it that way then all is well.

 

UIImageView *favoriteImage = (UIImageView *)[cell viewWithTag:10];

favoriteImage.hidden = ![[cell.boundObject valueForKey:@"favorite"] isEqualToString: @"Yes"];

Wg


  • Tarek and designwerks like this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#5 designwerks

designwerks

    Advanced Member

  • STV 5.0 Std
  • PipPipPipPipPip
  • 141 posts
  • LocationKingsville, Ontario, Canada
Reputation: 23
Excellent

Posted 27 October 2015 - 11:15 PM

wizgod,

 

Just used your code and it is working as expected.  You solved my problem.  I am studying your code to learn in reference to how yours works but my code did not.  You are a genius! Thank you so much.  

 

Can you explain the "!" in "favoriteImage.hidden = !?  I am unclear what this means in the expression...

 

I think this added detail will be bonus for the end user and create a happy experience.

 

John

 

P.S. here is a simple screen cap to show the result... if others wish to use in their apps...

 

Attached File  Simulator Screen Shot Oct 28, 2015, 3.13.36 AM.png   786.99K   31 downloads

 

 

 

 



#6 designwerks

designwerks

    Advanced Member

  • STV 5.0 Std
  • PipPipPipPipPip
  • 141 posts
  • LocationKingsville, Ontario, Canada
Reputation: 23
Excellent

Posted 27 October 2015 - 11:41 PM

Answered my own question with google... My understanding is that the "!" is or means "NOT"... makes sense...



#7 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 27 October 2015 - 11:47 PM

yup.. ! is a NOT function.. you got it


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#8 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 28 October 2015 - 07:29 AM

:D

 

You might want to think of making it an image button that the user can just toggle from the cell; that way they don't have to navigate all the way down to the edit screen just to flip the favorite flag.

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users