由于我们的tableview中加载了数据库中的很多实体.但是我们在tableView中仅仅是显示了名字,我们更需要的是知道详情.这就需要我们的设置一个DetailViewController了.来,搞起~
1.我们先来看看 detailView视图的模样
2.关于视图的设置我就不多说了,你可以查看一下 static 和group等属性 ,摆弄一下
下面是我们的detailViewController.h文件
//
// LCHeroDetailController.h
// myCoreData
//
// Created by lichan on 13-12-5.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LCHeroDetailController : UITableViewController
//因为我们这个detail就是用来显示某一个 NSManagerObject属性的,我们就声明了一个object hero~
@property (strong,nonatomic)NSManagedObject *hero;
@end
3.看到.m文件,我分段讲解
1)viewDidLoad //由于我们的数据模型是存储在plist中的.我们先来看看plist的模样
为什么这样子设置>?这是符合我们的视图里面的排版的,能看明白么?
#import "LCHeroDetailController.h"
@interface LCHeroDetailController ()
@property (strong,nonatomic)NSArray *sections;
//我们定义了一个私有的属性,因为我们只在这个.m文件中使用.并在 viedidload中赋值
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *plistURL = [[NSBundle mainBundle]URLForResource:@"HeroDetailConfiguration" withExtension:@"plist"];//找到我们的plist文件的路径,便于我们查找到我们想要的数据
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfURL:plistURL];
self.sections = [plist valueForKey:@"sections"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
2)关于cell的绘制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
//这的 style: UITableViewCellStyleValue2 是相当重要的哦~
NSUInteger sectionIndex = [indexPath section];
NSUInteger rowIndex = [indexPath row];
NSDictionary *section = [self.sections objectAtIndex:sectionIndex];
NSArray *rows = [section objectForKey:@"rows"];
NSDictionary *row = [rows objectAtIndex:rowIndex];
cell.textLabel.text = [row objectForKey:@"label"];
cell.detailTextLabel.text = [[self.hero valueForKey:[row objectForKey:@"key"]]description];
}
// Configure the cell...
return cell;
}
里面的实现都是很简单的.我们还有两个协议关于行 和列的方法没实现?为什么? 因为我们的都是static的cell,所以就不需要动态的改变行列~~~