Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableViewController to display a persons information -- need assistance with editing
    text
    copied!<p>I have a UITableViewController that is being called when the user clicks on a row in the previous ViewController (which shows all contacts for the app). </p> <p>This view that I'm working on needs to show detailed information. Right now I'm only working with </p> <ul> <li>First Name</li> <li>Last Name</li> <li>Home Email</li> <li>Work Email</li> </ul> <p>Eventually I will add in much more information but this is my proof of concept data I want to work with.</p> <p><img src="https://i.stack.imgur.com/bOTTx.png" alt="Here is what the Scene looks like"></p> <p>The information passed into this scene is populated from a <strong>Person</strong> object.</p> <p>I'm trying to figure out how I can do a few things when the user hits Edit.</p> <ol> <li>I need to be able to show all the information that can be entered. So if the Person object only has "First Name", "Last Name" defined, then I also want to show that the other two email fields are available.</li> <li>To piggy back off of the first issue I'm having, I would like to hide the fields if they are not present (outside of editing)</li> <li>I want to be able to delete a field and be able to delete the whole Person object (maybe a button at the bottom like iOS7 contacts).</li> </ol> <p>I just need some assistance or a push in the right direction.</p> <hr> <p>Here is the code that I have for this ViewController</p> <pre><code>// // SingleContactViewController.h // General view to display a single person record #import &lt;UIKit/UIKit.h&gt; #import "PublicContactsViewController.h" #import "Person.h" @interface SingleContactViewController : UITableViewController &lt;ADBannerViewDelegate&gt; @property (nonatomic, strong) Person *person; @property (strong, nonatomic) IBOutlet UIBarButtonItem *editButton; @property (nonatomic, assign, getter=isPrivate) BOOL private; @property (strong, nonatomic) IBOutlet UITextField *firstNameTextField; @property (strong, nonatomic) IBOutlet UITextField *lastNameTextField; @property (strong, nonatomic) IBOutlet UITextField *homeEmailTextField; @property (strong, nonatomic) IBOutlet UITextField *workEmailTextField; @end </code></pre> <hr> <pre><code>// // SingleContactViewController.m // #import "SingleContactViewController.h" #import "Person.h" @interface SingleContactViewController () @property (strong, nonatomic) IBOutlet ADBannerView *banner; @property (nonatomic, assign) BOOL isEditing; - (IBAction)popBackToContacts:(UIBarButtonItem *)sender; - (IBAction)editContact:(UIBarButtonItem *)sender; @end @implementation SingleContactViewController - (void)viewWillAppear:(BOOL)animated { self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:.498 green:0 blue:.0 alpha:1]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _banner.delegate = self; NSLog(@"SingleContactView - viewDidLoad method: person = %@",self.person.firstName); self.firstNameTextField.text = [self.person.firstName copy]; self.lastNameTextField.text = [self.person.lastName copy]; self.homeEmailTextField.text = [self.person.homeEmail copy]; self.workEmailTextField.text = [self.person.workEmail copy]; } #pragma mark - Editing Methods - (void)setEditing:(BOOL)editing animated:(BOOL)animated { NSLog(@"Entered setEditing"); [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:animated]; if (editing == YES){ // Change views to edit mode. } else { // } } - (IBAction)editContact:(UIBarButtonItem *)sender { NSLog(@"User pressed 'Edit' button. Entered editContact method"); if ([self.tableView isEditing]) { // If the tableView is already in edit mode, turn it off. Also change the title of the button to reflect the intended verb (‘Edit’, in this case). UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonSystemItemDone target:self action:@selector(editContact:)]; self.navigationItem.rightBarButtonItem = newButton; _editButton = newButton; [self.tableView setEditing:NO animated:YES]; } else { UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonSystemItemEdit target:self action:@selector(editContact:)]; self.navigationItem.rightBarButtonItem = newButton; _editButton = newButton; [self.tableView setEditing:YES animated:YES]; } } #pragma mark - Navigation Methods - (IBAction)popBackToContacts:(UIBarButtonItem *)sender { [self.navigationController popViewControllerAnimated:YES]; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Perform Delete // Animate the deletion [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade]; } } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload