[IOS开发进阶与实战]第三天:使用CoreData 填充TabelView->点击Section实现Detail视图

由于我们的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,所以就不需要动态的改变行列~~~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值