浅谈iOS设计中用XML解析美团外卖地址信息并在tableView上显示

 

 

思路与步骤:

1、制作.XML文件(美团外卖)

=====》

======》点击“获取城市列表”

====》点击网址

====》进入页面把所有的内容复制到编辑文本  做成.xml 类型的文件

 

做好.xml文件后  把它拖入工程中就可以进行解析了

 

如果网络状态非常好的话 , 也可以不用制作.xml文件  直接对网址进行网络XML解析就行;

 

 

下面我主要介绍对.xml文件的解析(xml解析)

 

代码实现如下;

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

}

 
3、第二页
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;
}
 

 

posted @ 2016-03-25 21:01  右手指尖轻轻触  阅读(289)  评论(0编辑  收藏  举报