淺談iOS設計中用XML解析美團外賣地址信息並在tableView上顯示

 

 

思路與步驟:git

一、製做.XML文件(美團外賣)網絡

=====》app

======》點擊「獲取城市列表」dom

====》點擊網址atom

====》進入頁面把全部的內容複製到編輯文本  作成.xml 類型的文件spa

 

作好.xml文件後  把它拖入工程中就能夠進行解析了代理

 

若是網絡狀態很是好的話 , 也能夠不用製做.xml文件  直接對網址進行網絡XML解析就行;xml

 

 

下面我主要介紹對.xml文件的解析(xml解析)對象

 

代碼實現以下;ci

1.
  AppDelegate.h
#import <UIKit/UIKit.h>
#import "ShowTableViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;

@end
 
AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[ShowTableViewController alloc]initWithStyle:UITableViewStyleGrouped]];
    return YES;
}
2.第一頁
ShowTableViewController.h

#import <UIKit/UIKit.h>
#import "SecondTableViewController.h"
@interface ShowTableViewController : UITableViewController<NSXMLParserDelegate>
//全局字符串  顯示字典的Value值
@property(strong,nonatomic)NSString *string;
//用於顯示全部數據的集合
@property(strong,nonatomic)NSMutableArray *Array;
//用於封裝集合元素
@property(strong,nonatomic)NSMutableDictionary *Dictionary;
 
@end
 
ShowTableViewController.m
//  XML解析(美團)
//
//  Created by scjy on 16/3/25.
//  Copyright © 2016年 lanco. All rights reserved.
//

#import "ShowTableViewController.h"

@interface ShowTableViewController ()

@end

@implementation ShowTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
self.title=@"城市列表";
   
    //獲取文件路徑信息
    NSString *path=[[NSBundle mainBundle]pathForResource:@"meituancitys" ofType:@"xml"];
    //封裝成data對象類型
    NSData *data=[NSData dataWithContentsOfFile:path];
    //實例化 paeser 對象
    NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data];
    //代理
    parser.delegate=self;
    //執行解析方法
    [parser parse];
  
   
   
   //惟一標識
   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
   
}
//開始
-(void)parserDidStartDocument:(NSXMLParser *)parser
{   NSLog(@"start");
    self.Array=[NSMutableArray array];
   
}
//結束
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    //解析結束  輸出解析結果
//    NSLog(@"%@",self.Array);
   
    NSLog(@"End");
   
}
//開始解析
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
    if ([elementName  isEqualToString:@"division"]) {
        self.Dictionary=[NSMutableDictionary dictionary];
       
        [self.Dictionary setDictionary:attributeDict];
    }
   
}

//結束解析
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"id"]||[elementName isEqualToString:@"name"]||[elementName isEqualToString:@"location"]||[elementName isEqualToString:@"timezone"]||[elementName isEqualToString:@"timezone_offset_gmt"]||[elementName isEqualToString:@"latitude"]||[elementName isEqualToString:@"longitude"])
       
    {
        [self.Dictionary setObject:self.string forKey:elementName];
    } else if([elementName isEqualToString:@"division"]){
        [self.Array addObject:self.Dictionary];
       
    }
}
//查找解析結果
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //將局部變量賦值給全局變量
    self.string=string;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}
//分區行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.Array.count;
}

//行數內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
   
    cell.textLabel.text=self.Array[indexPath .row][@"name"];
    cell.textLabel.font=[UIFont systemFontOfSize:30];
    cell.textLabel.textColor=[UIColor colorWithRed:arc4random()%220/256.0 green:arc4random()%23/256.0 blue:arc4random()%215/256.0 alpha:1];
    if (indexPath.row%3==0) {
        cell.textLabel.textAlignment=NSTextAlignmentLeft;
    }else if (indexPath.row%3==1) {
        cell.textLabel.textAlignment=NSTextAlignmentCenter;
    } else {
        cell.textLabel.textAlignment=NSTextAlignmentRight;
    }

   
    cell.accessoryType=1;
//    cell.detailTextLabel.text=self.Array[indexPath.row][@"id"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondTableViewController *secondTVC=[[SecondTableViewController alloc]init];
//  傳值到下一頁
   
secondTVC.nameStr=self.Array[indexPath.row][@"name"];
secondTVC.latitudeStr=self.Array[indexPath.row][@"latitude"];
secondTVC.longitudeStr=self.Array[indexPath.row][@"longitude"];
secondTVC.idStr=self.Array[indexPath.row][@"id"];
secondTVC.timezone_offset_gmtStr=self.Array[indexPath.row][@"timezone_offset_gmt"];
   
secondTVC.timezonedStr=self.Array[indexPath.row][@"timezone"];
//切換到下一頁
    [self.navigationController pushViewController:secondTVC animated:YES];

}

 
三、第二頁
SecondTableViewController.h

#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController
//接收參數的屬性
@property(strong,nonatomic)NSString *idStr;//id地址

@property(strong,nonatomic)NSString *latitudeStr;//緯度
@property(strong,nonatomic)NSString *longitudeStr;//經度
@property(strong,nonatomic)NSString *nameStr;//名稱
@property(strong,nonatomic)NSString *timezonedStr;//時區
@property(strong,nonatomic)NSString *timezone_offset_gmtStr;

@property(strong,nonatomic)NSMutableArray *Array;
@property(strong,nonatomic) NSMutableArray *arr;
@end
 
SecondTableViewController.m

#import "SecondTableViewController.h"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=self.nameStr;
    //Value值
    self.Array=[NSMutableArray array];
    [self.Array addObject:self.nameStr];
    [self.Array addObject:self.idStr];
    [self.Array addObject:self.latitudeStr];
    [self.Array addObject:self.longitudeStr];
    [self.Array addObject:self.timezonedStr];
    [self.Array addObject:self.timezone_offset_gmtStr];
   
//    NSLog(@"self.Array=%@",self.Array);
    //關鍵字
    self.arr=[NSMutableArray array];
    [self.arr addObject:@"名稱:"];
    [self.arr addObject:@"地址:"];
    [self.arr addObject:@"緯度:"];
    [self.arr addObject:@"經度:"];
    [self.arr addObject:@"時區:"];
    [self.arr addObject:@"時區偏移量:"];
   
   


   
  //惟一標識
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

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

    return self.Array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *index=@"reuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:index];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:3 reuseIdentifier:index];
    }
   
    cell.textLabel.text=self.arr[indexPath.row];
    cell.textLabel.textColor=[UIColor colorWithRed:arc4random()%220/256.0 green:arc4random()%23/256.0 blue:arc4random()%215/256.0 alpha:1];//自由配色
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    //副標題
    cell.detailTextLabel.text=self.Array[indexPath.row];
    return cell;
}
相關文章
相關標籤/搜索