1、FKAppDelegate类
//.h
#import <UIKit/UIKit.h>
@class FKBookViewController;
@interface FKAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) FKBookViewController *viewController;
@property (strong, nonatomic) UINavigationController *naviController;
@end
//.m
#import "FKAppDelegate.h"
#import "FKBookViewController.h"
@implementation FKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
self.viewController = [[FKBookViewController alloc]
initWithStyle:UITableViewStyleGrouped];
// 创建UINavigationController对象
// 该UINavigationController以self.viewController为视图栈最底层控件
self.naviController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
// 设置窗口以self.naviController为根视图控制器
self.window.rootViewController = self.naviController;
[self.window makeKeyAndVisible];
return YES;
}
2、FKBookViewController类
//.h
#import <UIKit/UIKit.h>
@interface FKBookViewController : UITableViewController
// 定义2个NSMutableArray对象,分别保存图书名和图书详情
@property (nonatomic , strong) NSMutableArray* books;
@property (nonatomic , strong) NSMutableArray* details;
@end
//.m
#import <QuartzCore/QuartzCore.h>
#import "FKBookViewController.h"
#import "FKEditViewController.h"
@interface FKBookViewController ()
@end
@implementation FKBookViewController
@synthesize books;
@synthesize details;
- (void)viewDidLoad
{
[super viewDidLoad];
// 创建、并初始化NSArray对象。
books = [NSMutableArray arrayWithObjects:@"疯狂Android讲义",
@"疯狂iOS讲义", @"疯狂Ajax讲义" , @"疯狂XML讲义", nil];
// 创建、并初始化NSArray对象。
details = [NSMutableArray arrayWithObjects:
@"长期雄踞各网店销量排行榜榜首的图书",
@"全面而详细的iOS开发图书",
@"Ajax开发图书" ,
@"系统介绍XML相关知识", nil];
// 设置当前视图关联的导航项的标题
self.navigationItem.title = @"图书列表";
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
// 该方法返回值决定各表格行的控件。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 为表格行定义一个静态字符串作为标示符
static NSString* cellId = @"cellId"; // ①
// 从可重用表格行的队列中取出一个表格行
UITableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier:cellId];
// 如果取出的表格行为nil
if(!cell)
{
// 创建一个UITableViewCell对象,使用UITableViewCellStyleSubtitle风格
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellId];
}
// 从IndexPath参数中获取当前行的行号
NSUInteger rowNo = indexPath.row;
// 取出books中索引为rowNo的元素作为UITableViewCell的文本标题
cell.textLabel.text = [books objectAtIndex:rowNo];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 取出details中索引为rowNo的元素作为UITableViewCell的详细内容
cell.detailTextLabel.text = [details objectAtIndex:rowNo];
return cell;
}
// 该方法的返回值决定指定分区内包含多少个表格行。
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section
{
// 由于该表格只有一个分区,直接返回books中集合元素个数代表表格的行数
return books.count;
}
// UITableViewDelegate定义的方法,当表格行右边的附件按钮被单击时激发该方法
- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
// 获取表格行号
NSInteger rowNo = indexPath.row;
FKEditViewController* editController = [[FKEditViewController alloc]
init];
// 将被单击表格行的数据传给editController控制器对象
editController.name = [books objectAtIndex:rowNo];
editController.detail = [details objectAtIndex:rowNo];
editController.rowNo = rowNo;
// 将editController压入UINavigationController管理的控制器栈中
[self.navigationController pushViewController:editController
animated:YES];
}
@end
3、FKEditViewController类
新建类,继承自UIViewController,并勾选xib选项
//.h
#import <UIKit/UIKit.h>
@interface FKEditViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *nameField;
@property (strong, nonatomic) IBOutlet UITextView *detailField;
- (IBAction)finish:(id)sender;
// 保存从上一个控制器传入数据的属性
@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* detail;
@property (nonatomic, assign) NSInteger rowNo;
@end
//.m
#import <QuartzCore/QuartzCore.h>
#import "FKBookViewController.h"
#import "FKEditViewController.h"
#import "FKAppDelegate.h"
@interface FKEditViewController ()
@end
@implementation FKEditViewController
- (void)viewWillAppear:(BOOL)animated
{
self.nameField.text = self.name;
self.detailField.text = self.detail;
// 设置默认不允许编辑
self.nameField.enabled = NO;
self.detailField.editable = NO;
// 设置边框
self.detailField.layer.borderWidth = 1.5;
self.detailField.layer.borderColor = [[UIColor grayColor] CGColor];
// 设置圆角
self.detailField.layer.cornerRadius = 4.0f;
self.detailField.layer.masksToBounds = YES;
// 创建一个UIBarButtonItem对象,作为界面的导航项右边的按钮
UIBarButtonItem* rightBn = [[UIBarButtonItem alloc]
initWithTitle:@"编辑"
style:UIBarButtonItemStyleBordered
target:self action:@selector(beginEdit:)];
self.navigationItem.rightBarButtonItem = rightBn;
}
- (void) beginEdit:(id) sender
{
// 如果该按钮的文本为“编辑”
if([[sender title] isEqualToString:@"编辑"])
{
// 设置nameField、detailField允许编辑
self.nameField.enabled = YES;
self.detailField.editable = YES;
// 设置按钮文本为“完成”
self.navigationItem.rightBarButtonItem.title = @"完成";
}
else
{
// 放弃作为第一响应者
[self.nameField resignFirstResponder];
[self.detailField resignFirstResponder];
// 获取应用程序委托对象
FKAppDelegate* appDelegate = [UIApplication
sharedApplication].delegate;
// 使用用户在第一个文本框中输入的内容替换viewController
// 的books集合中指定位置的元素
[appDelegate.viewController.books replaceObjectAtIndex:
self.rowNo withObject:self.nameField.text];
// 使用用户在第一个文本框中输入的内容替换viewController
// 的details集合中指定位置的元素
[appDelegate.viewController.details replaceObjectAtIndex:
self.rowNo withObject:self.detailField.text];
// 设置nameField、detailField不允许编辑
self.nameField.enabled = NO;
self.detailField.editable = NO;
// 设置按钮文本为“编辑”
self.navigationItem.rightBarButtonItem.title = @"编辑";
}
}
- (IBAction)finish:(id)sender {
// 放弃作为第一响应者
[sender resignFirstResponder];
}
@end