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

标签 ios objective-c uitableview editing

我有一个 UITableViewController,当用户单击前一个 ViewController 中的一行(显示应用程序的所有联系人)时,就会调用该 UITableViewController。

我正在处理的这个 View 需要显示详细信息。 现在我只与

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

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

Here is what the Scene looks like

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

我试图弄清楚当用户点击“编辑”时如何做一些事情。

  1. 我需要能够显示所有可以输入的信息。因此,如果 Person 对象仅定义了“名字”、“姓氏”,那么我还想显示其他两个电子邮件字段可用。
  2. 为了解决我遇到的第一个问题,我想隐藏不存在的字段(在编辑之外)
  3. 我希望能够删除一个字段并能够删除整个 Person 对象(可能是底部的按钮,如 iOS7 联系人)。

我只需要一些帮助或朝正确方向插入。


这是我为此 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

最佳答案

我还没有浏览过你的代码。根据您请求帮助的问题,我说:

  1. 使用表格 View 显示用户联系方式详细信息。 (我看到已经完成了)

  2. 当用户点击“编辑”时,会将用户带到新的 View Controller (没有动画和隐藏导航栏),以便用户不了解页面的更改。

  3. 向用户显示联系人的所有属性(如果之前已填充或未填充),获取值并显示它们以供编辑。单击“完成编辑”时,将他带回用户详细信息 View Controller (无动画)并更新 TableView (对于哪些属性,值存在或不为零或不为空,这解决了第二个问题)。所以用户不会知道单独的编辑页面,但你的目的已经解决了


现在第三个问题来了,

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

关于ios - UITableViewController 显示人员信息 - 需要编辑帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501614/

相关文章:

ios - 正则表达式在 iOS 上无法正常工作

iphone - 如何将我的应用链接到 App Store 开发者页面?

objective-c - DecimalNumberByAdding 如何工作?

ios - 对齐 UITableViewHeaderFooterView 的 detailTextLabel

ios - NSSortDescriptor 不按字母顺序排序

ios - 如何使核心数据反向关系不是两个引用都为零

iOS - 在 UITextView 中编辑文本会更改属性

javascript - UIWebView 将用户名和密码传递到表单中

ios - UITableViewCell 自定义编辑 View

ios - 如何根据图像大小设置高度的单元格