详细的单元格包含很多控件,并且数据更加复杂,需要一个自定义Object-c 数据对象存放将在列表中显示的各个实例。
一个自定义Object-c 数据对象一个是父视图和子视图
#import <Foundation/Foundation.h>
#define kPresidentNumberKey @"President"
#define kPresidentNameKey @"Name"
#define kPresidentFromKey @"FromYear"
#define kPresidentToKey @"ToYear"
#define kPresidentPartyKey @"Party"
@interface BIDPresident :NSObject<NSCoding>
@property int number;
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *fromYear;
@property (nonatomic,copy)NSString *toYear;
@property (nonatomic,copy)NSString *party;
@end
#import "BIDPresident.h"
@implementation BIDPresident
@synthesize number;
@synthesize name;
@synthesize fromYear;
@synthesize toYear;
@synthesize party;
#pragma mark -
#pragma mark NSCoding
-(void)encodeWithCoder:(NSCoder *)Coder
{
[Coder encodeInt:self.numberforKey:kPresidentNumberKey];
[Coder encodeObject:self.nameforKey:kPresidentNameKey];
[Coder encodeObject:self.fromYearforKey:kPresidentFromKey];
[Coder encodeObject:self.toYearforKey:kPresidentToKey];
[Coder encodeObject:self.partyforKey:kPresidentPartyKey];
}
-(id)initWithCoder:(NSCoder *)coder
{
if (self=[superinit]) {
number=[coderdecodeIntForKey:kPresidentNumberKey];
name=[coderdecodeObjectForKey:kPresidentNameKey];
fromYear =[coderdecodeObjectForKey:kPresidentFromKey];
toYear =[coderdecodeObjectForKey:kPresidentToKey];
party =[coderdecodeObjectForKey:kPresidentPartyKey];
}
return self;
}
@end
父视图
#import "BIDSecondLevelViewController.h"
@interface BIDPresidentsViewController :BIDSecondLevelViewController
@property (strong,nonatomic)NSMutableArray *list;
@end
#import "BIDPresidentsViewController.h"
#import "BIDPresidentDetailController.h"
#import "BIDPresident.h"
@implementation BIDPresidentsViewController
@synthesize list;
-(void)viewDidLoad
{
[superviewDidLoad];
NSString *path=[[NSBundlemainBundle]pathForResource:@"Presidents"ofType:@"plist"];
NSData *data;
NSKeyedUnarchiver *unarchiver;
//对象存放临时编码初始化后并存放到数组中
data =[[NSDataalloc]initWithContentsOfFile:path];
unarchiver= [[NSKeyedUnarchiveralloc]initForReadingWithData:data];
NSMutableArray *array = [unarchiverdecodeObjectForKey:@"Presidents"];
self.list =array;
[unarchiverfinishDecoding];
}
-(void)viewWillAppear:(BOOL)animated
{
//当用户更改了详细信息视图中的内容,为了确保父视图重新加载这些新数据。只是做了强制重新加载。
[superviewWillAppear:animated];
[self.tableViewreloadData];
}
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listcount];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *PresidentListCellIdentifier=@"PresidentListCellIdentifier";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:PresidentListCellIdentifier];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:PresidentListCellIdentifier];
}
NSInteger row =[indexPathrow];
BIDPresident *thePres=[self.listobjectAtIndex:row];
cell.textLabel.text=thePres.name;
cell.detailTextLabel.text=[NSStringstringWithFormat:@"%@-%@",thePres.fromYear,thePres.toYear];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row =[indexPathrow];
BIDPresident *prez =[self.listobjectAtIndex:row];
BIDPresidentDetailController *childController =[[BIDPresidentDetailControlleralloc]initWithStyle:UITableViewStyleGrouped];
childController.title=prez.name;
childController.president =prez;//这里必须时detail表h文件声明后才会出现
[self.navigationControllerpushViewController:childControlleranimated:YES];
}
@end
子视图
#import <UIKit/UIKit.h>
@class BIDPresident;
#define kNumberOfEditableRows 4
#define kNameRowIndex 0
#define kFromYearRowIndex 1
#define kToYearRowIndex 2
#define kPartyIndex 3
#define kLabelTag 4096
@interface BIDPresidentDetailController :UITableViewController
<UITextFieldDelegate>
@property (strong,nonatomic)BIDPresident *president;
@property (strong,nonatomic)NSArray *filedLabels;
@property(strong,nonatomic)NSMutableDictionary *tempValues;
@property (strong,nonatomic)UITextField *currentTextFiled;
-(IBAction)cancel:(id)sender;
-(IBAction)save:(id)sender;
-(IBAction)textFieldDone:(id)sender;
@end
#import "BIDPresidentDetailController.h"
#import "BIDPresident.h"
@implementation BIDPresidentDetailController
@synthesize president;
@synthesize filedLabels;
@synthesize tempValues;
@synthesize currentTextFiled;
-(IBAction)cancel:(id)sender
{
[self.navigationControllerpopViewControllerAnimated:YES];//单击按钮Cancel当前视图出栈,下个视图升到栈的顶部
}
-(IBAction)save:(id)sender
{
//检查是否当前有正被编辑的文本字段,有的话将值存放到tempvalues中
if (currentTextFiled !=nil) {
NSNumber* tagAsNum=[NSNumbernumberWithInt:currentTextFiled.tag];
[tempValuessetObject:currentTextFiled.textforKey:tagAsNum];
}
//遍历字段中的键值对,将tempvalues数组的适当值分配给president指定字段
for(NSNumber *keyin [tempValuesallKeys])
{
switch ([keyintValue]) {
casekNameRowIndex:
president.name=[tempValuesobjectForKey:key];
break;
casekFromYearRowIndex:
president.fromYear=[tempValuesobjectForKey:key];
break;
casekToYearRowIndex:
president.toYear=[tempValuesobjectForKey:key];
break;
casekPartyIndex:
president.party=[tempValuesobjectForKey:key];
break;
default:
break;
}
}
//返回列表
[self.navigationControllerpopViewControllerAnimated:YES];
//重新加载数据
NSArray *allController=self.navigationController.viewControllers;
UITableViewController *parent =[allControllerlastObject];
[parent.tableViewreloadData];
}
-(IBAction)textFieldDone:(id)sender
{
//当用户单击Done后关闭键盘
[sender resignFirstResponder];
}
#pragma mark -
-(void) viewDidLoad
{
[superviewDidLoad];
NSArray *array=[[NSArrayalloc]initWithObjects:@"Name",@"From",@"To",@"Party",nil];
self.filedLabels=array;
UIBarButtonItem *cancelButton=[[UIBarButtonItemalloc]initWithTitle:@"Cancel"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(cancel:)];
self.navigationItem.leftBarButtonItem =cancelButton;
UIBarButtonItem *saveButton=[[UIBarButtonItemalloc]initWithTitle:@"Save"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(save:)];
self.navigationItem.rightBarButtonItem=saveButton;
NSMutableDictionary *dict =[[NSMutableDictionaryalloc]init];
self.tempValues=dict;
}
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnkNumberOfEditableRows;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *PresidentCellIdentifier=@"PresidentCellIdentifier";
UITableViewCell *cell= [tableViewdequeueReusableCellWithIdentifier:PresidentCellIdentifier];
if (cell ==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:PresidentCellIdentifier];
//创建一个单元的时候会创建一个标签,然后创建一个文本字段
UILabel *label= [[UILabelalloc]initWithFrame:CGRectMake(10,10,75, 25)];
label.textAlignment=UITextAlignmentRight;
label.tag=kLabelTag;
label.font=[UIFontboldSystemFontOfSize:14];
[cell.contentViewaddSubview:label];
UITextField *textField=[[UITextFieldalloc]initWithFrame:CGRectMake(90,12,200, 25)];
textField.clearsOnBeginEditing=NO;
[textFieldsetDelegate:self];
textField.returnKeyType =UIReturnKeyDone;//键盘的返回键类型
[textField addTarget:selfaction:@selector(textFieldDone:)forControlEvents:UIControlEventEditingDidEndOnExit];//调用Did End on Exit事件
[cell.contentViewaddSubview:textField];
}
NSUInteger row =[indexPathrow];
UILabel *label =(UILabel *) [cellviewWithTag:kLabelTag];
UITextField *textField =nil;
for(UIView *oneViewin cell.contentView.subviews)
{
if ([oneViewisMemberOfClass:[UITextFieldclass]]) {
textField=(UITextField *)oneView;
}
}
label.text=[filedLabelsobjectAtIndex:row];
NSNumber *rowAsNum=[NSNumbernumberWithInt:row];
switch (row) {
casekNameRowIndex:
if ([[tempValuesallKeys]containsObject:rowAsNum]) {
textField.text=[tempValuesobjectForKey:rowAsNum];
}
else
textField.text=president.name;
break;
casekFromYearRowIndex:
if ([[tempValuesallKeys]containsObject:rowAsNum]) {
textField.text=[tempValuesobjectForKey:rowAsNum];
}
else
textField.text=president.fromYear;
break;
casekToYearRowIndex:
if ([[tempValuesallKeys]containsObject:rowAsNum]) {
textField.text=[tempValuesobjectForKey:rowAsNum];
}
else
textField.text=president.toYear;
break;
casekPartyIndex:
if ([[tempValuesallKeys]containsObject:rowAsNum]) {
textField.text=[tempValuesobjectForKey:rowAsNum];
}
else
textField.text=president.party;
break;
default:
break;
}
if (currentTextFiled==textField) {
currentTextFiled=nil;
}
textField.tag=row;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
-(NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
returnnil;//该方法将在行被选中时调用,并且允许禁止选中行。作用仅仅是不让行在选中之后呈突出显示
}
#pragma mark Text Filed Delegate Methods
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
self.currentTextFiled=textField;//当文本字段称为第一响应者时调用此方法。
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
//当用户通过单击不同文本字段或者按下Done按钮来停止编辑当前文本字段时,调用该方法。将该字段的值保存在tempValues字段中。
NSNumber *tagAsNum=[NSNumbernumberWithInt:textField.tag];
[tempValuessetObject:textField.textforKey:tagAsNum];
}
@end
在FirstLevelController.m中条件
//BIDPresident View/Edit
BIDPresidentsViewController *presidentsViewController=[[BIDPresidentsViewControlleralloc]initWithStyle:UITableViewStylePlain];
presidentsViewController.title=@"Detail Edit";
presidentsViewController.rowImage=[UIImageimageNamed:@"detailEditIcon.png"];
[arrayaddObject:presidentsViewController];