我有一个联系人应用程序,我有一个视图,用户可以单击该视图查看用户的“详细”信息。在此详细视图中,它仅显示具有值的字段,并隐藏其余字段。当用户单击“编辑”时,它将显示允许用户编辑的所有字段。
让我展示一下这个
的一些屏幕
我正在使用第三方StaticDataTableViewController类来隐藏和显示单元格。
这是我的实际问题 我提出this post我问的是如何更动态地处理电子邮件和电话号码,而不是定义“工作电子邮件”,“家庭电子邮件”,“工作电话”,“家庭电话”。
我需要找出一种显示和表示动态生成的电子邮件/电话号码的方法。如果用户有5个电话号码,那么我想显示5个电话号码加上他们的标签(家庭,工作)。我将所有这些存储在NSDurray的NSArray中(key = label,value = email / phone),每个联系人将有两个NSArray。
我只需要在如何动态和静态的同时获得一些帮助?我不想失去隐藏细胞的功能。
我将粘贴处理表视图的类。
SingleContactViewController.h
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import "PublicContactsViewController.h"
#import "Person.h"
#import "SingleContactTableViewCell.h"
#import "StaticDataTableViewController.h"
#import "DTAlertView.h"
@interface SingleContactViewController : StaticDataTableViewController <UITextFieldDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) Person *person;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *editButton;
// TextFields are embedded in the cell, was just easier to make an outlet
@property (strong, nonatomic) IBOutlet UITextField *firstNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *lastNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *homeEmailTextField;
@property (strong, nonatomic) IBOutlet UITextField *workEmailTextField;
@property (strong, nonatomic) IBOutlet UITextField *companyNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *homePhoneNumberTextField;
@property (strong, nonatomic) IBOutlet UITextField *workPhoneNumberTextField;
@property (strong, nonatomic) IBOutlet UITextField *cellPhoneNumberTextField;
// Outlet to cells to hide/unhide
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *firstNameCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *lastNameCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *homeEmailCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *workEmailCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *companyNameCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *homePhoneNumberCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *workPhoneNumberCell;
@property (weak, nonatomic) IBOutlet SingleContactTableViewCell *cellPhoneNumberCell;
@property (strong, nonatomic) IBOutlet SingleContactTableViewCell *deleteContactCell;
- (IBAction)deleteContact:(UIButton *)sender;
@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
#pragma mark - Lifecycle methods
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:.498 green:0 blue:.0 alpha:1];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.firstNameTextField.delegate = self;
[self populateFieldsAndHideEmptyCells];
//Disable the highlight effect of clicking a table row
[self.firstNameCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[self.lastNameCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.homeEmailCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.workEmailCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.companyNameCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.homePhoneNumberCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.workPhoneNumberCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.cellPhoneNumberCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.deleteContactCell setSelectionStyle:UITableViewCellEditingStyleNone];
[self.tableView setBackgroundColor:[UIColor lightGrayColor]];
}
#pragma mark - Editing Methods
- (IBAction)editContact:(UIBarButtonItem *)sender {
NSLog(@"User pressed 'Edit' button. Entered editContact method");
if ([self isEditing]) {
NSLog(@"Turning edit more off");
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonSystemItemDone target:self action:@selector(editContact:)];
self.navigationItem.rightBarButtonItem = newButton;
_editButton = newButton;
[self updatePrivateContact];
self.isEditing = NO;
}
else {
NSLog(@"Turning edit mode on");
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonSystemItemEdit target:self action:@selector(editContact:)];
self.navigationItem.rightBarButtonItem = newButton;
_editButton = newButton;
self.isEditing = YES;
[self showAllFieldsForEditing];
}
}
#pragma mark - Hide/Show Table Cells
- (void)populateFieldsAndHideEmptyCells
{
self.hideSectionsWithHiddenRows = YES;
if([self.person.firstName length] == 0){
NSLog(@"firstName not present. Hide Cell");
[self cell:self.firstNameCell setHidden:YES];
}else{
self.firstNameTextField.text = [self.person firstName];
[self.firstNameTextField setClearButtonMode:UITextFieldViewModeNever];
[self.firstNameTextField setEnabled:NO];
}
if([self.person.lastName length] == 0){
NSLog(@"lastName not present. Hide Cell");
[self cell:self.lastNameCell setHidden:YES];
}else{
self.lastNameTextField.text = [self.person lastName];
[self.lastNameTextField setClearButtonMode:UITextFieldViewModeNever];
[self.lastNameTextField setEnabled:NO];
}
if([self.person.companyName length] == 0){
[self cell:self.companyNameCell setHidden:YES];
}else{
self.companyNameTextField.text = [self.person companyName];
[self.companyNameTextField setClearButtonMode:UITextFieldViewModeNever];
[self.companyNameTextField setEnabled:NO];
}
if([self.person.homeEmail length] == 0){
[self cell:self.homeEmailCell setHidden:YES];
}else{
self.homeEmailTextField.text = [self.person homeEmail];
[self.homeEmailTextField setClearButtonMode:UITextFieldViewModeNever];
[self.homeEmailTextField setEnabled:NO];
}
NSLog(@"person.workemail: %@",[self.person workEmail]);
if([self.person.workEmail length] == 0){
[self cell:self.workEmailCell setHidden:YES];
}else{
self.workEmailTextField.text = [self.person workEmail];
[self.workEmailTextField setClearButtonMode:UITextFieldViewModeNever];
[self.workEmailTextField setEnabled:NO];
}
if([self.person.homePhone length] == 0){
NSLog(@"home phone field not present. Hide Cell");
[self cell:self.homePhoneNumberCell setHidden:YES];
}else{
self.homePhoneNumberTextField.text = [self.person homePhone];
[self.homePhoneNumberTextField setClearButtonMode:UITextFieldViewModeNever];
[self.homePhoneNumberTextField setEnabled:NO];
}
if([self.person.workPhone length] == 0){
NSLog(@"work phone field not present. Hide Cell");
[self cell:self.workPhoneNumberCell setHidden:YES];
}else{
self.workPhoneNumberTextField.text = [self.person workPhone];
[self.workPhoneNumberTextField setClearButtonMode:UITextFieldViewModeNever];
[self.workPhoneNumberTextField setEnabled:NO];
}
if([self.person.cellPhone length] == 0){
NSLog(@"cell phone field not present. Hide Cell");
[self cell:self.cellPhoneNumberCell setHidden:YES];
}else{
self.cellPhoneNumberTextField.text = [self.person cellPhone];
[self.cellPhoneNumberTextField setClearButtonMode:UITextFieldViewModeNever];
[self.cellPhoneNumberTextField setEnabled:NO];
}
[self cell:self.deleteContactCell setHidden:YES]; // Only show on Edit
[self reloadDataAnimated:YES];
}
- (void) showAllFieldsForEditing
{
NSLog(@"showAllFieldsForEditing method entered");
self.hideSectionsWithHiddenRows = NO;
[self cell:self.firstNameCell setHidden:NO];
[self cell:self.lastNameCell setHidden:NO];
[self cell:self.companyNameCell setHidden:NO];
[self cell:self.homeEmailCell setHidden:NO];
[self cell:self.workEmailCell setHidden:NO];
[self cell:self.homePhoneNumberCell setHidden:NO];
[self cell:self.workPhoneNumberCell setHidden:NO];
[self cell:self.cellPhoneNumberCell setHidden:NO];
[self cell:self.deleteContactCell setHidden:NO];
[self.firstNameTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.lastNameTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.companyNameTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.homeEmailTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.workEmailTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.homePhoneNumberTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.workPhoneNumberTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.cellPhoneNumberTextField setClearButtonMode:UITextFieldViewModeUnlessEditing];
[self.firstNameTextField setEnabled:YES];
[self.lastNameTextField setEnabled:YES];
[self.companyNameTextField setEnabled:YES];
[self.workEmailTextField setEnabled:YES];
[self.homeEmailTextField setEnabled:YES];
[self.homePhoneNumberTextField setEnabled:YES];
[self.workPhoneNumberTextField setEnabled:YES];
[self.cellPhoneNumberTextField setEnabled:YES];
[self reloadDataAnimated:YES];
}
#pragma mark - Navigation Methods
- (IBAction)popBackToContacts:(UIBarButtonItem *)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table View Methods
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
#pragma mark - Update Edit Data
- (void)updatePrivateContact
{
// Logic here to save to database
}
#pragma mark - Helper Methods
- (BOOL)isAnyEmailPresent
{
if([self.person homeEmail] != nil || [self.person workEmail] != nil){
return YES;
}else{
return NO;
}
}
- (Person *)populatePersonToSave
{
Person *person = [[Person alloc] init];
person.firstName = self.firstNameTextField.text;
person.lastName = self.lastNameTextField.text;
person.workEmail = self.workEmailTextField.text;
person.homeEmail = self.homeEmailTextField.text;
person.companyName = self.companyNameTextField.text;
person.homePhone = self.homePhoneNumberTextField.text;
person.workPhone = self.workPhoneNumberTextField.text;
person.cellPhone = self.cellPhoneNumberTextField.text;
return person;
}
#pragma mark - Table View Methods
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor lightGrayColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Set the text color of our header/footer text.
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
// Set the background color of our header/footer.
header.contentView.backgroundColor = [UIColor lightGrayColor];
// You can also do this to set the background color of our header/footer,
// but the gradients/other effects will be retained.
// view.tintColor = [UIColor blackColor];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
return nil;
} else {
return [super tableView:tableView titleForHeaderInSection:section];
}
}
@end
答案 0 :(得分:1)
。 StaticDataTableViewController使用tableview根据自己的“OriginalRow”类显示您希望它显示的单元格。
它们保存在属性“rows”下的“OriginalSection”类中的Mutable数组中。
原始部分保存在名为“sections”的属性下的“OriginalTable”类的可变数组中
原始表是您的表应包含的主要定义,并保存在名为“originalTable”的属性下的“StaticDataTableViewController”
确定要显示哪些单元格以及要隐藏哪些单元格的所有业务都在“ - [OriginalTable prepareUpdates]”中完成,它遍历各个部分和每行内部以确定要显示的内容和隐藏的内容。
“ - [OriginalTable sections]”数组和“ - [OriginalSection rows]”数组分别继续包含所有节和行,无论其可见性或隐身性如何,因此如果您编辑这些数组并调用“ - [StaticDataTableViewController reloadData] “你将获得你正在寻找的行动。
要完成此操作,您需要将每个类的“@interface”移动到标题,以便您可以访问它。
当你将OriginalRow,OriginalSection和OriginalTable的“@interface”移动到标题时,你就是那里的大部分。
最后要做的是将属性从StaticDataTableViewController的私有类别移动到公共接口。
@interface StaticDataTableViewController ()
@property (nonatomic, strong) OriginalTable * originalTable; // Cut this line.
@end
取标记的行并将其移动到公共接口的标题中,使其成为公共属性
@interface StaticDataTableViewController : UITableViewController // Paste after this line
完成这些更改后,您应该能够随意添加和删除Sections和Rows。
[[[[ThisStaticTable.originalTable sections] objectAtIndex:0] rows]
insertObject:myNewOriginalRow atIndex:2];
[ThisStaticTable reloadData];
这样可以帮助你顺利完成。