【发布时间】:2013-12-10 17:47:00
【问题描述】:
我有一个 UITableViewController,当用户单击前一个 ViewController 中的一行时会调用它(显示应用程序的所有联系人)。
我正在处理的这个视图需要显示详细信息。 现在我只和
- 名字
- 姓氏
- 家庭邮箱
- 工作邮箱
最终我会添加更多信息,但这是我想要使用的概念验证数据。
传递到此场景的信息是从 Person 对象填充的。
我试图弄清楚当用户点击编辑时我可以如何做一些事情。
- 我需要能够显示所有可以输入的信息。因此,如果 Person 对象仅定义了“First Name”、“Last Name”,那么我还想显示其他两个电子邮件字段可用。
- 为了解决我遇到的第一个问题,如果字段不存在(编辑之外),我想隐藏它们
- 我希望能够删除一个字段并能够删除整个 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