سؤال

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).

This view that I'm working on needs to show detailed information. Right now I'm only working with

  • First Name
  • Last Name
  • Home Email
  • Work Email

Eventually I will add in much more information but this is my proof of concept data I want to work with.

Here is what the Scene looks like

The information passed into this scene is populated from a Person object.

I'm trying to figure out how I can do a few things when the user hits Edit.

  1. 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.
  2. 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)
  3. 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).

I just need some assistance or a push in the right direction.


Here is the code that I have for this ViewController

//
//  SingleContactViewController.h
//  General view to display a single person record

#import <UIKit/UIKit.h>
#import "PublicContactsViewController.h"
#import "Person.h"

@interface SingleContactViewController : UITableViewController <ADBannerViewDelegate>

@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

//
//  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
هل كانت مفيدة؟

المحلول 2

I discovered this StaticDataTableViewControlller and it resolved my issue immediately. I appreciate the help in this post though!

نصائح أخرى

I have not gone through your code. according to the problem you have asked your assistance for am saying:

  1. show user contact details using a table view. (which is done i see)

  2. when user hits 'edit' take user to a new view Controller (without animation and hiding navigation bar) so that user do not understand the change of page.

  3. show user all the attributes of contacts (if previously filled or unfilled), get values and show them for edit., when 'done edit' is clicked lead him back to user detail view-controller(without animating) and update the table View(for which attributes the values are present or not nil or not blank, this solves the second problem). so user does not come to know about the separate edit page but your purpose is solved


now comes the third problem,

  1. delete the rows using 'commitEditingStyle' method, and update the datasource, and accordingly change the numberOfSectionsInTableView and numberOfRowsInSection methods. else you will get range exceptions error
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top