IOS-read plist files
// UI-city list
//
// Created by jzq_macon 15/7/30.
// Copyright (c) jzq_mac. All rights reserved.
//
# Import ViewController. h
# Import DetailViewController. h
@ Interface ViewController ()
{
NSArray * allCitys;
UITableView * myTableView;
}
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
[Self loadData];
[Self creatTableView];
}
# Pragma ---------------------- obtain data in plist ----------------------
-(Void) loadData {
NSString * path = [[NSBundle mainBundle] pathForResource: @ citys. plist ofType: nil];
AllCitys = [NSArray arrayWithContentsOfFile: path];
NSLog (% @, allCitys );
}
# Pragma -------------------- initialize 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; // unique cell identifier
// TableView: Check whether there is a cell called cellID (full screen)
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellID];
// Initialize the cell if no result is found.
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];
// Mode switch ViewController; used to temporarily switch to another ViewController and put the ViewController to be displayed on the top of the presentedViewController
// Disable dismissViewControllerAnimated when you no longer need to put the ViewController on the top.
Detail. modalTransitionStyle = UIModalTransitionStylePartialCurl;
[Self presentViewController: detail animated: YES completion: nil];
}