Implementing a UIImageView in a UITableViewCell only when certain conditions are met with Core Data and NSFetchedResultsController

StackOverflow https://stackoverflow.com/questions/22954747

Question

I'm relatively new to iOS development and I have a question to which I believe the answer is easy, but I can't seem to find a way to achieve it.

I have a UITableViewController which is populated by the user tapping on a button on the NavigationBar where the user fills in the Name, Event, Sub-Event, Amount, Date, Status and Optionally Notes. The data is saved to CoreData and the UITableView is populated with NSFetchedResultsController.

The notes are completely optional and it takes the form of a UITextView, where the other text based entries are UITextFields.

The notes attribute is part of a Transaction Entity and is a NSString.

Because there's no way (that I can see) to have a placeholder for the UITextView, I've set up the default text to say "Additional Notes: " with the space at the end.

In my UITableViewController, I want the UIImageView (that represents the note) to only appear if:

1) The user types something completely different to "Additional Notes: " &

2) If the length of the notes textView is bigger than 0

So in my UITableViewController cellForRow, I have:

if (![transaction.notes isEqualToString:@"Additional Notes: "] && [transaction.notes length] > 0)
{
    UIImageView *pin = [[UIImageView alloc] initWithFrame:CGRectMake(13, 30, 24, 25)];
    pin.image = [UIImage imageNamed:@"pin"];
    [customCell addSubview:pin];
}

That's working to some extent. If I add text to "Additional Notes: ", it adds the PIN to the cell.

Problems

If I remove a few letters in the notes, so "Additional Not ", it treats the if statement as true and therefore attaches a pin to the statement.

If I have a note called "Additional Notes: This is a test note", it shows the PIN in the cell. However, if I go to edit that entry and remove "This is a test note", so it's back to the default, it doesn't remove the PIN from the cell of this UITableViewController.

So my questions are:

1) Can I have a placeholder for the notes? That would solve all of my issues

2) If not, how can I achieve this process efficiently, so it's not misleading for the users thinking they have a note if they just removed one or two letters

3) Have updated notes reflect appropriately in the UITableViewController

Any guidance would be really appreciated

Was it helpful?

Solution

Use this :-

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    if([[textView text] isEqualToString:@"Additional Notes: "]){
        [textView setText:@""];
    }
    return YES;

}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {

    if([[[textView text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]){
        [textView setText:@"Additional Notes: "];
    }
    return YES;
}

This will handle the whitespaces and new lines only scenario.

OTHER TIPS

the first approach: You need set text default, and change its color: gray ..smt like placeholder of textfield. Then your cell implement Textview delegate:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (void)textViewDidChange:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; 

textViewShouldBegin: to remove the default textview

textViewDidChange: check your lenght textview >0 & text != defaulttext

textShouldChange..: check like textViewDidChange and In case: you are typing, and still editing in your cell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top