导航控制器、表格视图与故事板开发指南
立即解锁
发布时间: 2025-08-24 01:11:18 阅读量: 2 订阅数: 7 

# 导航控制器、表格视图与故事板开发指南
## 1. 实现详细视图列表控制器
### 1.1 修改 BIDPresidentsViewController.h
首先,我们需要对 `BIDPresidentsViewController.h` 进行修改,使其符合 `BIDPresidentDetailViewControllerDelegate` 协议,并添加一个可变数组属性 `presidents` 用于存储总统信息。以下是具体代码:
```objective-c
#import "BIDSecondLevelViewController.h"
#import "BIDPresidentDetailViewController.h"
@interface BIDPresidentsViewController : BIDSecondLevelViewController
<BIDPresidentDetailViewControllerDelegate>
@property (strong, nonatomic) NSMutableArray *presidents;
@end
```
### 1.2 修改 BIDPresidentsViewController.m
接下来,切换到 `BIDPresidentsViewController.m` 文件,进行一系列的修改。这里主要涉及到初始化方法、视图加载方法、表格视图数据源方法、表格视图代理方法以及总统详情视图代理方法的实现。
```objective-c
#import "BIDPresidentsViewController.h"
#import "BIDPresident.h"
#import "BIDPresidentDetailViewController.h"
static NSString *CellIdentifier = @"Cell";
@interface BIDPresidentsViewController ()
@end
@implementation BIDPresidentsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// 自定义初始化
self.title = @"Detail Edit";
self.rowImage = [UIImage imageNamed:@"detailEditIcon.png"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents"
ofType:@"plist"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
self.presidents = [unarchiver decodeObjectForKey:@"Presidents"];
[unarchiver finishDecoding];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 在视图加载后进行额外的设置
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:CellIdentifier];
}
#pragma mark - 表格视图数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.presidents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
BIDPresident *president = self.presidents[indexPath.row];
cell.textLabel.text = president.name;
return cell;
}
#pragma mark - 表格视图代理方法
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BIDPresident *president = self.presidents[indexPath.row];
BIDPresidentDetailViewController *controller =
[[BIDPresidentDetailViewController alloc] init];
controller.president = president;
controller.delegate = self;
controller.row = indexPath.row;
[self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - 总统详情视图代理方法
- (void)presidentDetailViewController:(BIDPresidentDetailViewController *)controller
didUpdatePresident:(BIDPresident *)president
{
[self.presidents replaceObjectAtIndex:controller.row withObject:president];
[self.tableView reloadData];
}
@end
```
### 1.3 代码解释
在 `initWithNibName:bundle:` 方法中,我们使用 `NSKeyedUnarchiver` 从属性列表文件中创建一个包含
0
0
复制全文
相关推荐










