UITableViewController显示人员信息 - 需要编辑帮助

时间:2013-12-10 17:47:00

标签: ios objective-c uitableview editing

我有一个UITableViewController,当用户点击上一个ViewController中的一行(显示该应用的所有联系人)时,就会调用该控件。

我正在研究的这个观点需要显示详细信息。 现在我只与

合作
  • 名字
  • 姓氏
  • 家庭电子邮件
  • 工作电子邮件

最终我会添加更多信息,但这是我想要使用的概念证据数据。

Here is what the Scene looks like

传递到此场景的信息是从 Person 对象填充的。

我正在试图找出当用户点击编辑时我可以做些什么。

  1. 我需要能够显示可以输入的所有信息。因此,如果Person对象仅定义了“First Name”,“Last Name”,那么我还想显示其他两个电子邮件字段是否可用。
  2. 为了躲开我遇到的第一个问题,我想隐藏这些字段(如果它们不存在)(编辑之外)
  3. 我希望能够删除一个字段并能够删除整个Person对象(可能是iOS7联系人底部的按钮)。
  4. 我只需要一些帮助或推动正确的方向。


    以下是我对此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 个答案:

答案 0 :(得分:1)

我没有查看你的代码。根据你问的帮助我问的问题:

  1. 使用表格视图显示用户联系详细信息。 (这是我看到的)

  2. 当用户点击“编辑”时,将用户带到新的视图控制器(没有动画和隐藏导航栏),以便用户不理解页面的更改。

  3. 向用户显示联系人的所有属性(如果先前已填充或未填充),获取值并显示它们以进行编辑。当点击“完成编辑”时,将他引回用户详细信息视图 - 控制器(无动画)并更新表视图(值存在或不存在的属性是否为空,这解决了第二个问题)。因此,用户不会了解单独的编辑页面,但您的目的已经解决


  4. 现在出现了第三个问题,

    1. 使用'commitEditingStyle'方法删除行,并更新数据源,并相应地更改numberOfSectionsInTableView和numberOfRowsInSection方法。否则你会得到范围例外错误

答案 1 :(得分:0)

我发现了这个StaticDataTableViewControlller,它立即解决了我的问题。 我很欣赏这篇文章中的帮助!