網(wǎng)址:http://www.meituan.com/api/v1/divisions
#import<UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
#import<UIKit/UIKit.h>
#import "SecondTableViewController.h"
@interface ViewController : UIViewController
/**
*? 全局的集合 用來添加字典的
*/
@property(strong,nonatomic) NSMutableArray *arrM;
/**
*? 字典顯示 元素名稱和value值
*/
@property(strong,nonatomic) NSMutableDictionary *dicM;
/**
*? str 實際上是 字典中的value值
*/
@property(strong,nonatomic) NSString *str;
@property(strong,nonatomic) UITableView *tableview;
/**
*? 城市名字
*/
@property(strong,nonatomic) NSMutableArray *arrMname;
/**
*? 城市拼音
*/
@property(strong,nonatomic) NSMutableArray *arrMid;
/**
*? 城市維度
*/
@property(strong,nonatomic) NSMutableArray *arrMlatitude;
/**
*? 城市經(jīng)度
*/
@property(strong,nonatomic) NSMutableArray *arrMlongitude;
/**
*? 存儲集合的字典
*/
@property(strong,nonatomic) NSDictionary *dictotal;
@end
#import "ViewController.h"@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
? [super viewDidLoad];
? //指定xml文件路徑?
? NSURL *url = [NSURL URLWithString:@"http://www.meituan.com/api/v1/divisions"];? ? //為 parser 指定初始化
? NSXMLParser *pareser = [[NSXMLParser alloc] initWithContentsOfURL:url];??
//指定代理??
pareser.delegate = self;?
? //實現(xiàn)文件xml解析 執(zhí)行代理方法?
? BOOL bol = [pareser parse];??
//=返回解析的結果 成功 或 失敗?
? NSLog(@"%d",bol);?
? //初始化集合? ?
self.arrMname = [NSMutableArray array];?
? //初始化tableview??
self.tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];?
? self.tableview.backgroundColor = [UIColor colorWithRed:1.000 green:0.942 blue:0.820 alpha:1.000];?
? //指定代理?
? self.tableview.dataSource = self;?
? self.tableview.delegate = self;??
//添加到父視圖??
[self.view addSubview:self.tableview];? ?
//顯示標題? ? self.title = @"美團城市";? ?
//設置導航欄上面的右邊按鈕? ?
UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithTitle:@"下一頁" style:UIBarButtonItemStylePlain target:self action:@selector(next)];? ? self.navigationItem.rightBarButtonItem = rightbutton;? ?
//設置導航欄的前景色? ?
[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.876 green:0.399 blue:0.225 alpha:1.000]];??
//設置導航欄標題的字體大小和字體顏色? ?
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1.000 green:0.350 blue:0.427 alpha:1.000],NSForegroundColorAttributeName,[UIFont fontWithName:@"Arial-Bold" size:30],NSFontAttributeName, nil]];??
}
//跳轉到下一頁
-(void)next
{? ?
SecondTableViewController *second = [[SecondTableViewController alloc] initWithStyle:UITableViewStylePlain];? ?
[self.navigationController pushViewController:second animated:YES];
}
/** *? 文檔解析開始 初始化全局的集合
* * @param parser
*/
-(void)parserDidStartDocument:(NSXMLParser *)parser
{? ?
self.arrM = [NSMutableArray array];
}
/** *? 文檔解析結束
* *? @param parser
*/
-(void)parserDidEndDocument:(NSXMLParser *)parser
{? ?
//輸出集合內(nèi)容? ?
//NSLog(@"%@",self.arrM);
}
/** *? 文檔元素 解析 開始
* *? @param parser? ? ? ? 解析的對象
*? @param elementName? 元素的名稱
*? @param namespaceURI? 命名空間
*? @param qName
*? @param attributeDict 屬性的字典
*/
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*)attributeDict
{
//找到文檔中User元素医增,開始初始化字典
if ([elementName isEqualToString:@"division"]) {
//初始化字典
self.dicM = [NSMutableDictionary dictionary];
//向字典中添加屬性元素
[self.dicM setDictionary:attributeDict];
}
}
/**
*? 文檔中解析結束
*
*? @param parser
*? @param elementName? 元素名稱
*? @param namespaceURI
*? @param qName
*/
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//判斷元素的關鍵字
if ([elementName isEqualToString:@"name"] || [elementName isEqualToString:@"id"] || [elementName isEqualToString:@"timezone"] || [elementName isEqualToString:@"timezone_offset_gmt"] || [elementName isEqualToString:@"latitude"] || [elementName isEqualToString:@"longitude"]) {
[self.dicM setObject:self.str forKey:elementName];
}
//元素標簽時才向集合中添加字典
else if ([elementName isEqualToString:@"division"]){
[self.arrM addObject:self.dicM];
}
}
/**
*? 解析文件內(nèi)容
*
*? @param parser 元素對象
*? @param string 顯示文本內(nèi)容
*/
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
self.str = string;
}
//顯示分區(qū)數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//每一個分區(qū)顯示的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrM.count;
}
//顯示每一行的內(nèi)容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *iden = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:iden];
}
//初始化集合
self.arrMid = [NSMutableArray array];
self.arrMlatitude = [NSMutableArray array];
self.arrMlongitude = [NSMutableArray array];
//初始化字典
self.dictotal = [NSDictionary dictionary];
for (_dictotal in self.arrM) {
//將關鍵字對應的value值添加到集合中
[self.arrMname addObject:_dictotal[@"name"]];
[self.arrMid addObject:_dictotal[@"id"]];
[self.arrMlatitude addObject:_dictotal[@"latitude"]];
[self.arrMlongitude addObject:_dictotal[@"longitude"]];
}
//顯示信息
cell.textLabel.text = self.arrMname[indexPath.row];
cell.detailTextLabel.text = self.arrMid[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//初始化類的對象
SecondTableViewController *second = [[SecondTableViewController alloc] initWithStyle:UITableViewStylePlain];
//獲取點擊的索引值
second.index = indexPath.row;
//將集合賦給下一頁要顯示的集合
second.arrMname = self.arrMname;
second.arrMlatitude = self.arrMlatitude;
second.arrMlongitude = self.arrMlongitude;
//跳轉到下一頁 (導航視圖)
[self.navigationController pushViewController:second animated:YES];
}
//顯示行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import<UIKit/UIKit.h>
@interface SecondTableViewController : UITableViewController
/**
*? 城市名集合
*/
@property(strong,nonatomic) NSMutableArray *arrMname;
/**
*? 相應城市的維度的集合
*/
@property(strong,nonatomic) NSMutableArray *arrMlatitude;
/**
*? 相應城市經(jīng)度的集合
*/
@property(strong,nonatomic) NSMutableArray *arrMlongitude;
/**
*? 從第一頁接收信息的集合
*/
@property(strong,nonatomic) NSMutableArray *arrRevice;
/**
*? 獲取索引值
*/
@property(assign,nonatomic) NSInteger index;
@end
#import "SecondTableViewController.h"
@interface SecondTableViewController ()
@end
@implementation SecondTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加背景色
self.view.backgroundColor = [UIColor colorWithRed:0.909 green:1.000 blue:0.482 alpha:1.000];
//設置導航欄的左按鈕
UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = leftbutton;
self.title = @"緯度與經(jīng)度";
//唯一標識的重用
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
}
/**
*? 返回上一頁
*/
-(void)back
{
//跳轉到上一頁
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
//顯示的分區(qū)數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie
{
return 3;
}
//每個分區(qū)顯示的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
//顯示每行的內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
if (indexPath.section == 0) {
cell.textLabel.text = self.arrMname[self.index];
}
else if(indexPath.section == 1) {
cell.textLabel.text = self.arrMlatitude[self.index];//緯度
}else
{
cell.textLabel.text = self.arrMlongitude[self.index];//經(jīng)度
}
return cell;
}
//顯示行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
//顯示頭部標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"顯示緯度與經(jīng)度";
}
return @"";
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end