您的位置:首页 > 移动开发 > IOS开发

iOS -读取plist文件

2015-07-31 08:29 501 查看
// 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查找有没有叫cellID的cell(满一屏的情况)

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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: