iOS -读取plist文件

来源:互联网 发布:工作日报表软件 编辑:程序博客网 时间:2024/04/28 03:31

//  UI-城市列表

//

//  Created by jzq_mac on 15/7/30.

//  Copyright (c) 2015 jzq_mac. All rights reserved.

//


#import "ViewController.h"

#import "DetailViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    NSArray *allCitys;

    UITableView *myTableView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self loadData];

    [self creatTableView];

    

    

    

   }




#pragma ----------------------获得plist里面的数据----------------------


- (void)loadData{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"citys.plist" ofType:nil];

    allCitys = [NSArray arrayWithContentsOfFile:path];

    NSLog(@"%@", allCitys);

  

}



#pragma ----------------------初始化TableView----------------------


- (void)creatTableView

{

    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-20) style:UITableViewStylePlain];

    myTableView.delegate = self;

    myTableView.dataSource = self;

    [self.view addSubview:myTableView];

}




#pragma ----------------------UITableViewDelegate---------------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return allCitys.count;

}



#pragma ----------------------UITableViewDataSource---------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *cellID = @"cityCell";//cell的唯一标识符

//  TableView查找有没有叫cellIDcell(满一屏的情况)

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

//    如果没有查找到就初始化cell

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

        

    }

    

    cell.textLabel.text = allCitys[indexPath.row][@"State"];

    return cell;

    

}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    DetailViewController *detail = [[DetailViewController alloc]init];

    

//    模态切换ViewController;用于临时切换到另一个ViewController,把需要显示的ViewController放在最上面 presentedViewController

//   当不再需要刚才放到最上面的ViewController的时候让他消失dismissViewControllerAnimated

    detail.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    [self presentViewController:detail animated:YES completion:nil];

   

}



0 0
原创粉丝点击