int currentAlbumIndex;
}
@end
现在,替换类扩展里的 @interface 这一行,完成后如下:
复制代码
@interface ViewController () <UITableViewDataSoure, UITableViewDelegate> {
这就是如何设置一个正确的委托─把它相象成允许一个委托来履行一个方法的合同。这里,表明 ViewController 将会遵照 UITableViewDataSource 和 UITableViewDelegate 协议。这种方法下 UITableView 必须执行它自己的委托方法。
下面,用下面的代码替换 viewDidLoad:
复制代码- (void)viewDidLoad {
[super viewDidLoad];
// 1
self.view.backgroundColor = [UIColor colorWithRed:0.76f green:0.81f blue:0.87f alpha:1];
currentAlbumIndex = 0;
//2
allAlbums = [[LibraryAPI sharedInstance] getAlbums];
// 3
// the uitableview that presents the album data
dataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStyleGrouped];
dataTable.delegate = self;
dataTable.dataSource = self;
dataTable.backgroundView = nil;
[self.view addSubview:dataTable];
}
这里分析下上面的代码:
把背景色改为漂亮的深蓝色。
从 API 获取一个列表,它包含所有的专辑数据。不能直接使用 PersistencyManager。
创建一个 UITableView。你声明了视图控制器是 UITableView delegate/data source;因此,UITableView 将会提供视图控制器需要的所有信息。
现在,在 ViewController.m 里面添加如下方法:
- (void)showDataForAlbumAtIndex:(int)albumIndex{
// defensive code: make sure the requested index is lower than the amount of albums
if (albumIndex < allAlbums.count) {
// fetch the album
Album *album = allAlbums[albumIndex];
// save the albums data to present it later in the tableview
currentAlbumData = [album tr_tableRepresentation];
} else {










