【问题标题】:UITableViewController to display a persons information -- need assistance with editingUITableViewController 显示人员信息 - 需要编辑帮助
【发布时间】:2013-12-10 17:47:00
【问题描述】:

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

我正在处理的这个视图需要显示详细信息。 现在我只和

  • 名字
  • 姓氏
  • 家庭邮箱
  • 工作邮箱

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

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

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

  1. 我需要能够显示所有可以输入的信息。因此,如果 Person 对象仅定义了“First Name”、“Last Name”,那么我还想显示其他两个电子邮件字段可用。
  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

【问题讨论】:

    标签: ios objective-c uitableview editing


    【解决方案1】:

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

    1. 使用表格视图显示用户联系方式。 (我看到了)

    2. 当用户点击“编辑”时,将用户带到一个新的视图控制器(没有动画和隐藏导航栏),这样用户就不会理解页面的变化。

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


    现在是第三个问题,

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

    【讨论】:

    • 除了“1)”之外,这都是正确的。我的意思是如果用户只有“名字”,我点击编辑我希望显示所有字段,以便用户可以添加新数据。
    • 啊,我想我现在明白了。我没有想到让另一个视图控制器成为编辑视图控制器。然后我可以有一个包含所有视图的表,只需填充我拥有的视图并将它们全部设置为“启用”,以便用户可以编辑我的文本字段。我需要查看有关如何在没有动画的情况下过渡到新视图控制器的 api。我想现在剩下的问题是如何只显示 UITable 中 Person 对象中填充的字段。如果对象中没有填充单元格,我可以隐藏它吗?如何以编程方式引用每个单元格?
    • 这次我不明白你的问题。请再来一次。如果它是这样的:您只想显示具有值的单元格(而不是未填充的单元格),那么只需首先创建一个 NSMutableArray 的所有联系人属性,这些属性确实具有值。然后使用数组作为 UITable 的数据源
    • 非常感谢您的帮助,但我认为我做错了静态单元格,我添加了我的分辨率。
    【解决方案2】:

    我发现了这个StaticDataTableViewControlller,它立即解决了我的问题。 不过,我感谢这篇文章中的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多