在这篇里,我们要学习的目标:
- 1. 解析远程的JSON数据
- 2. 分页显示数据
- 3. 学习Category的使用
概述
IOS5.0开始支持JSON,所以不用第三方的解析了,主要用于这三个对象:NSDictionary、NSData、NSJSONSerializatio
今天我们做一个天气预报数据的展示,通过国家气象局提供的天气预报接口来获取数据。注:以实现功能为主,不做过多的界面上展示
接口地址:
- http://www.weather.com.cn/data/sk/101010100.html
- http://www.weather.com.cn/data/cityinfo/101010100.html
- http://m.weather.com.cn/data/101010100.html
以第三个地址显示内容最全,我们就以这个地获取数据,返回的数据格式如下:
View Code1 {
2 "weatherinfo":{
3 "city":"北京",
4 "city_en":"beijing",
5 "date_y":"2012年9月5日",
6 "date":"",
7 "week":"星期三",
8 "fchh":"11",
9 "cityid":"101010100",
10 "temp1":"27℃~17℃",
11 "temp2":"24℃~17℃",
12 "temp3":"25℃~17℃",
13 "temp4":"27℃~16℃",
14 "temp5":"28℃~17℃",
15 "temp6":"26℃~17℃",
16 "tempF1":"80.6℉~62.6℉",
17 "tempF2":"75.2℉~62.6℉",
18 "tempF3":"77℉~62.6℉",
19 "tempF4":"80.6℉~60.8℉",
20 "tempF5":"82.4℉~62.6℉",
21 "tempF6":"78.8℉~62.6℉",
22 "weather1":"晴转阴",
23 "weather2":"阵雨",
24 "weather3":"阵雨转多云",
25 "weather4":"多云转晴",
26 "weather5":"晴",
27 "weather6":"多云转阵雨",
28 "img1":"0",
29 "img2":"2",
30 "img3":"3",
31 "img4":"99",
32 "img5":"3",
33 "img6":"1",
34 "img7":"1",
35 "img8":"0",
36 "img9":"0",
37 "img10":"99",
38 "img11":"1",
39 "img12":"3",
40 "img_single":"0",
41 "img_title1":"晴",
42 "img_title2":"阴",
43 "img_title3":"阵雨",
44 "img_title4":"阵雨",
45 "img_title5":"阵雨",
46 "img_title6":"多云",
47 "img_title7":"多云",
48 "img_title8":"晴",
49 "img_title9":"晴",
50 "img_title10":"晴",
51 "img_title11":"多云",
52 "img_title12":"阵雨",
53 "img_title_single":"晴",
54 "wind1":"微风",
55 "wind2":"微风",
56 "wind3":"微风",
57 "wind4":"微风",
58 "wind5":"微风",
59 "wind6":"微风",
60 "fx1":"微风",
61 "fx2":"微风",
62 "fl1":"小于3级",
63 "fl2":"小于3级",
64 "fl3":"小于3级",
65 "fl4":"小于3级",
66 "fl5":"小于3级",
67 "fl6":"小于3级",
68 "index":"暖",
69 "index_d":"较凉爽,建议着长袖衬裤等春秋过渡装。体弱者宜着长袖衬衫和马甲。但昼夜温差较大,请适当增减衣服。",
70 "index48":"暖",
71 "index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
72 "index_uv":"中等",
73 "index48_uv":"弱",
74 "index_xc":"不宜",
75 "index_tr":"适宜",
76 "index_co":"较舒适",
77 "st1":"26",
78 "st2":"17",
79 "st3":"22",
80 "st4":"16",
81 "st5":"23",
82 "st6":"17",
83 "index_cl":"适宜",
84 "index_ls":"适宜",
85 "index_ag":"易发"
86 }
87 }
Category 是Objective-C 里面最常用到的功能之一。简单的讲Category可以为已经存在的类增加方法,而不需要增加一个子类。而且,我们可以在不知道某个类内部实现的情况下,为该类增加方法。如果我们想增加某个框架(framework)中的类的方法,Category就非常有效。比如,今天的内容中,就想在NSDictionary上增加一个方法来直接处理URL远程数据,后面有详细代码。 定义格式如下:
1 #import " 类名.h "
2 @interface 类名(类别名)
3 // 新方法的声明
4 @end
具体操作,如下步骤:
1. 创建项目,项目名称:listCityWeather
2. 添加一个Category类,扩展NSDictionary,主要方便于处理远程的json数据
2.1 修改NSDictionary+Json.h文件
2 // NSDictionary+Json.h
3 // listCityWeather
4 //
5 // Created by tony on 12-9-5.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 @interface NSDictionary (Json)
12
13
14 // 直把远程的地址上Json数据转,换成Dictionary对象
15 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress;
16
17 // 把当前的Dictionary对象,转成Json对象
18 -(NSData*)toJSON;
19
20 @end
2.2 修改NSDictionary+Json.m文件
2 // NSDictionary+Json.m
3 // listCityWeather
4 //
5 // Created by tony on 12-9-5.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import " NSDictionary+Json.h "
10
11 @implementation NSDictionary (Json)
12
13 // 直把远程的地址上Json数据转,换成Dictionary对象
14 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress
15 {
16 // 请求远程数据,存放到NSData对象中
17 NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];
18
19 // 定义一个错误信息的对象
20 __autoreleasing NSError *error =nil;
21
22 // 序列化字符串
23 id result =[NSJSONSerialization JSONObjectWithData:data
24 options:kNilOptions error:&error];
25 if(error !=nil)
26 return nil;
27
28 return result;
29 }
30
31 // 把当前的Dictionary对象,转成Json对象
32 -(NSData*)toJSON{
33 NSError *error =nil;
34 // 把当前的Dictionary对象转换成字符串
35 id result =[NSJSONSerialization dataWithJSONObject:self
36 options:kNilOptions error:&error];
37 if(error !=nil)
38 return nil;
39
40 return result;
41 }
42
43 @end
2.3 调用方法如下:
2
3 NSLog( @" city: %@ ",[[city valueForKey: @" weatherinfo "] valueForKey: @" city "] );
4
5 /
6
7 NSDictionary *myInfo =[NSDictionary dictionaryWithObjectsAndKeys: @" tony ", @" apple ", @" anny ", @" ada ",nil];
8 NSData *json =[myInfo toJSON];
9
10 NSLog( @" json: %@ ",[[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding]);
3. 添加plist数据, 地区资料,我就懒得做服务器端程序,这里就用一个plist数据来展示中国有天气的城市列表
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>北京</key>
<string>101010100</string>
<key>朝阳</key>
<string>101071201</string>
<key>顺义</key>
<string>101010400</string>
<key>怀柔</key>
<string>101010500</string>
<key>通州</key>
<string>101010600</string>
<key>昌平</key>
<string>101010700</string>
<key>延庆</key>
<string>101010800</string>
<key>丰台</key>
<string>101010900</string>
<key>石景山</key>
<string>101011000</string>
<key>大兴</key>
<string>101011100</string>
<key>房山</key>
<string>101011200</string>
<key>密云</key>
<string>101011300</string>
<key>门头沟</key>
<string>101011400</string>
<key>平谷</key>
<string>101011500</string>
<key>八达岭</key>
<string>101011600</string>
<key>佛爷顶</key>
<string>101011700</string>
<key>汤河口</key>
<string>101011800</string>
<key>密云上甸子</key>
<string>101011900</string>
<key>斋堂</key>
<string>101012000</string>
<key>霞云岭</key>
<string>101012100</string>
<key>北京城区</key>
<string>101012200</string>
<key>海淀</key>
<string>101010200</string>
<key>天津</key>
<string>101030100</string>
<key>宝坻</key>
<string>101030300</string>
<key>东丽</key>
<string>101030400</string>
<key>西青</key>
<string>101030500</string>
<key>北辰</key>
<string>101030600</string>
<key>蓟县</key>
<string>101031400</string>
<key>汉沽</key>
<string>101030800</string>
<key>静海</key>
<string>101030900</string>
<key>津南</key>
<string>101031000</string>
<key>塘沽</key>
<string>101031100</string>
<key>大港</key>
<string>101031200</string>
<key>武清</key>
<string>101030200</string>
<key>宁河</key>
<string>101030700</string>
<key>上海</key>
<string>101020100</string>
<key>宝山</key>
<string>101020300</string>
<key>嘉定</key>
<string>101020500</string>
<key>南汇</key>
<string>101020600</string>
<key>浦东</key>
<string>101021300</string>
<key>青浦</key>
<string>101020800</string>
<key>松江</key>
<string>101020900</string>
<key>奉贤</key>
<string>101021000</string>
<key>崇明</key>
<string>101021100</string>
<key>徐家汇</key>
<string>101021200</string>
<key>闵行</key>
<string>101020200</string>
<key>金山</key>
<string>101020700</string>
<key>石家庄</key>
<string>101090101</string>
<key>张家口</key>
<string>101090301</string>
<key>承德</key>
<string>101090402</string>
<key>唐山</key>
<string>101090501</string>
<key>秦皇岛</key>
<string>101091101</string>
<key>沧州</key>
<string>101090701</string>
<key>衡水</key>
<string>101090801</string>
<key>邢台</key>
<string>101090901</string>
<key>邯郸</key>
<string>101091001</string>
<key>保定</key>
<string>101090201</string>
<key>廊坊</key>
<string>101090601</string>
<key>郑州</key>
<string>101180101</string>
<key>新乡</key>
<string>101180301</string>
<key>许昌</key>
<string>101180401</string>
<key>平顶山</key>
<string>101180501</string>
<key>信阳</key>
<string>101180601</string>
<key>南阳</key>
<string>101180701</string>
<key>开封</key>
<string>101180801</string>
<key>洛阳</key>
<string>101180901</string>
<key>商丘</key>
<string>101181001</string>
<key>焦作</key>
<string>101181101</string>
<key>鹤壁</key>
<string>101181201</string>
<key>濮阳</key>
<string>101181301</string>
<key>周口</key>
<string>101181401</string>
<key>漯河</key>
<string>101181501</string>
<key>驻马店</key>
<string>101181601</string>
<key>三门峡</key>
<string>101181701</string>
<key>济源</key>
<string>101181801</string>
<key>安阳</key>
<string>101180201</string>
<key>合肥</key>
<string>101220101</string>
<key>芜湖</key>
<string>101220301</string>
<key>淮南</key>
<string>101220401</string>
<key>马鞍山</key>
<string>101220501</string>
<key>安庆</key>
<string>101220601</string>
<key>宿州</key>
<string>101220701</string>
<key>阜阳</key>
<string>101220801</string>
<key>亳州</key>
<string>101220901</string>
<key>黄山</key>
<string>101221001</string>
<key>滁州</key>
<string>101221101</string>
<key>淮北</key>
<string>101221201</string>
<key>铜陵</key>
<string>101221301</string>
<key>宣城</key>
<string>101221401</string>
<key>六安</key>
<string>101221501</string>
<key>巢湖</key>
<string>101221601</string>
<key>池州</key>
<string>101221701</string>
<key>蚌埠</key>
<string>101220201</string>
<key>杭州</key>
<string>101210101</string>
<key>舟山</key>
<string>101211101</string>
<key>湖州</key>
<string>101210201</string>
<key>嘉兴</key>
<string>101210301</string>
<key>金华</key>
<string>101210901</string>
<key>绍兴</key>
<string>101210501</string>
<key>台州</key>
<string>101210601</string>
<key>温州</key>
<string>101210701</string>
<key>丽水</key>
<string>101210801</string>
<key>衢州</key>
<string>101211001</string>
<key>宁波</key>
<string>101210401</string>
<key>重庆</key>
<string>101040100</string>
<key>合川</key>
<string>101040300</string>
<key>南川</key>
<string>101040400</string>
<key>江津</key>
<string>101040500</string>
<key>万盛</key>
<string>101040600</string>
<key>渝北</key>
<string>101040700</string>
<key>北碚</key>
<string>101040800</string>
<key>巴南</key>
<string>101040900</string>
<key>长寿</key>
<string>101041000</string>
<key>黔江</key>
<string>101041100</string>
<key>万州天城</key>
<string>101041200</string>
<key>万州龙宝</key>
<string>101041300</string>
<key>涪陵</key>
<string>101041400</string>
<key>开县</key>
<string>101041500</string>
<key>城口</key>
<string>101041600</string>
<key>云阳</key>
<string>101041700</string>
<key>巫溪</key>
<string>101041800</string>
<key>奉节</key>
<string>101041900</string>
<key>巫山</key>
<string>101042000</string>
<key>潼南</key>
<string>101042100</string>
<key>垫江</key>
<string>101042200</string>
<key>梁平</key>
<string>101042300</string>
<key>忠县</key>
<string>101042400</string>
<key>石柱</key>
<string>101042500</string>
<key>大足</key>
<string>101042600</string>
<key>荣昌</key>
<string>101042700</string>
<key>铜梁</key>
<string>101042800</string>
<key>璧山</key>
<string>101042900</string>
<key>丰都</key>
<string>101043000</string>
<key>武隆</key>
<string>101043100</string>
<key>彭水</key>
<string>101043200</string>
<key>綦江</key>
<string>101043300</string>
<key>酉阳</key>
<string>101043400</string>
<key>秀山</key>
<string>101043600</string>
<key>沙坪坝</key>
<string>101043700</string>
<key>永川</key>
<string>101040200</string>
<key>福州</key>
<string>101230101</string>
<key>泉州</key>
<string>101230501</string>
<key>漳州</key>
<string>101230601</string>
<key>龙岩</key>
<string>101230701</string>
<key>晋江</key>
<string>101230509</string>
<key>南平</key>
<string>101230901</string>
<key>厦门</key>
<string>101230201</string>
<key>宁德</key>
<string>101230301</string>
<key>莆田</key>
<string>101230401</string>
<key>三明</key>
<string>101230801</string>
<key>兰州</key>
<string>101160101</string>
<key>平凉</key>
<string>101160301</string>
<key>庆阳</key>
<string>101160401</string>
<key>武威</key>
<string>101160501</string>
<key>金昌</key>
<string>101160601</string>
<key>嘉峪关</key>
<string>101161401</string>
<key>酒泉</key>
<string>101160801</string>
<key>天水</key>
<string>101160901</string>
<key>武都</key>
<string>101161001</string>
<key>临夏</key>
<string>101161101</string>
<key>合作</key>
<string>101161201</string>
<key>白银</key>
<string>101161301</string>
<key>定西</key>
<string>101160201</string>
<key>张掖</key>
<string>101160701</string>
<key>广州</key>
<string>101280101</string>
<key>惠州</key>
<string>101280301</string>
<key>梅州</key>
<string>101280401</string>
<key>汕头</key>
<string>101280501</string>
<key>深圳</key>
<string>101280601</string>
<key>珠海</key>
<string>101280701</string>
<key>佛山</key>
<string>101280800</string>
<key>肇庆</key>
<string>101280901</string>
<key>湛江</key>
<string>101281001</string>
<key>江门</key>
<string>101281101</string>
<key>河源</key>
<string>101281201</string>
<key>清远</key>
<string>101281301</string>
<key>云浮</key>
<string>101281401</string>
<key>潮州</key>
<string>101281501</string>
<key>东莞</key>
<string>101281601</string>
<key>中山</key>
<string>101281701</string>
<key>阳江</key>
<string>101281801</string>
<key>揭阳</key>
<string>101281901</string>
<key>茂名</key>
<string>101282001</string>
<key>汕尾</key>
<string>101282101</string>
<key>韶关</key>
<string>101280201</string>
<key>南宁</key>
<string>101300101</string>
<key>柳州</key>
<string>101300301</string>
<key>来宾</key>
<string>101300401</string>
<key>桂林</key>
<string>101300501</string>
<key>梧州</key>
<string>101300601</string>
<key>防城港</key>
<string>101301401</string>
<key>贵港</key>
<string>101300801</string>
<key>玉林</key>
<string>101300901</string>
<key>百色</key>
<string>101301001</string>
<key>钦州</key>
<string>101301101</string>
<key>河池</key>
<string>101301201</string>
<key>北海</key>
<string>101301301</string>
<key>崇左</key>
<string>101300201</string>
<key>贺州</key>
<string>101300701</string>
<key>贵阳</key>
<string>101260101</string>
<key>安顺</key>
<string>101260301</string>
<key>都匀</key>
<string>101260401</string>
<key>兴义</key>
<string>101260906</string>
<key>铜仁</key>
<string>101260601</string>
<key>毕节</key>
<string>101260701</string>
<key>六盘水</key>
<string>101260801</string>
<key>遵义</key>
<string>101260201</string>
<key>凯里</key>
<string>101260501</string>
<key>昆明</key>
<string>101290101</string>
<key>红河</key>
<string>101290301</string>
<key>文山</key>
<string>101290601</string>
<key>玉溪</key>
<string>101290701</string>
<key>楚雄</key>
<string>101290801</string>
<key>普洱</key>
<string>101290901</string>
<key>昭通</key>
<string>101291001</string>
<key>临沧</key>
<string>101291101</string>
<key>怒江</key>
<string>101291201</string>
<key>香格里拉</key>
<string>101291301</string>
<key>丽江</key>
<string>101291401</string>
<key>德宏</key>
<string>101291501</string>
<key>景洪</key>
<string>101291601</string>
<key>大理</key>
<string>101290201</string>
<key>曲靖</key>
<string>101290401</string>
<key>保山</key>
<string>101290501</string>
<key>呼和浩特</key>
<string>101080101</string>
<key>乌海</key>
<string>101080301</string>
<key>集宁</key>
<string>101080401</string>
<key>通辽</key>
<string>101080501</string>
<key>阿拉善左旗</key>
<string>101081201</string>
<key>鄂尔多斯</key>
<string>101080701</string>
<key>临河</key>
<string>101080801</string>
<key>锡林浩特</key>
<string>101080901</string>
<key>呼伦贝尔</key>
<string>101081000</string>
<key>乌兰浩特</key>
<string>101081101</string>
<key>包头</key>
<string>101080201</string>
<key>赤峰</key>
<string>101080601</string>
<key>南昌</key>
<string>101240101</string>
<key>上饶</key>
<string>101240301</string>
<key>抚州</key>
<string>101240401</string>
<key>宜春</key>
<string>101240501</string>
<key>鹰潭</key>
<string>101241101</string>
<key>赣州</key>
<string>101240701</string>
<key>景德镇</key>
<string>101240801</string>
<key>萍乡</key>
<string>101240901</string>
<key>新余</key>
<string>101241001</string>
<key>九江</key>
<string>101240201</string>
<key>吉安</key>
<string>101240601</string>
<key>武汉</key>
<string>101200101</string>
<key>黄冈</key>
<string>101200501</string>
<key>荆州</key>
<string>101200801</string>
<key>宜昌</key>
<string>101200901</string>
<key>恩施</key>
<string>101201001</string>
<key>十堰</key>
<string>101201101</string>
<key>神农架</key>
<string>101201201</string>
<key>随州</key>
<string>101201301</string>
<key>荆门</key>
<string>101201401</string>
<key>天门</key>
<string>101201501</string>
<key>仙桃</key>
<string>101201601</string>
<key>潜江</key>
<string>101201701</string>
<key>襄樊</key>
<string>101200201</string>
<key>鄂州</key>
<string>101200301</string>
<key>孝感</key>
<string>101200401</string>
<key>黄石</key>
<string>101200601</string>
<key>咸宁</key>
<string>101200701</string>
<key>成都</key>
<string>101270101</string>
<key>自贡</key>
<string>101270301</string>
<key>绵阳</key>
<string>101270401</string>
<key>南充</key>
<string>101270501</string>
<key>达州</key>
<string>101270601</string>
<key>遂宁</key>
<string>101270701</string>
<key>广安</key>
<string>101270801</string>
<key>巴中</key>
<string>101270901</string>
<key>泸州</key>
<string>101271001</string>
<key>宜宾</key>
<string>101271101</string>
<key>内江</key>
<string>101271201</string>
<key>资阳</key>
<string>101271301</string>
<key>乐山</key>
<string>101271401</string>
<key>眉山</key>
<string>101271501</string>
<key>凉山</key>
<string>101271601</string>
<key>雅安</key>
<string>101271701</string>
<key>甘孜</key>
<string>101271801</string>
<key>阿坝</key>
<string>101271901</string>
<key>德阳</key>
<string>101272001</string>
<key>广元</key>
<string>101272101</string>
<key>攀枝花</key>
<string>101270201</string>
<key>银川</key>
<string>101170101</string>
<key>中卫</key>
<string>101170501</string>
<key>固原</key>
<string>101170401</string>
<key>石嘴山</key>
<string>101170201</string>
<key>吴忠</key>
<string>101170301</string>
<key>西宁</key>
<string>101150101</string>
<key>黄南</key>
<string>101150301</string>
<key>海北</key>
<string>101150801</string>
<key>果洛</key>
<string>101150501</string>
<key>玉树</key>
<string>101150601</string>
<key>海西</key>
<string>101150701</string>
<key>海东</key>
<string>101150201</string>
<key>海南</key>
<string>101150401</string>
<key>济南</key>
<string>101120101</string>
<key>潍坊</key>
<string>101120601</string>
<key>临沂</key>
<string>101120901</string>
<key>菏泽</key>
<string>101121001</string>
<key>滨州</key>
<string>101121101</string>
<key>东营</key>
<string>101121201</string>
<key>威海</key>
<string>101121301</string>
<key>枣庄</key>
<string>101121401</string>
<key>日照</key>
<string>101121501</string>
<key>莱芜</key>
<string>101121601</string>
<key>聊城</key>
<string>101121701</string>
<key>青岛</key>
<string>101120201</string>
<key>淄博</key>
<string>101120301</string>
<key>德州</key>
<string>101120401</string>
<key>烟台</key>
<string>101120501</string>
<key>济宁</key>
<string>101120701</string>
<key>泰安</key>
<string>101120801</string>
<key>西安</key>
<string>101110101</string>
<key>延安</key>
<string>101110300</string>
<key>榆林</key>
<string>101110401</string>
<key>铜川</key>
<string>101111001</string>
<key>商洛</key>
<string>101110601</string>
<key>安康</key>
<string>101110701</string>
<key>汉中</key>
<string>101110801</string>
<key>宝鸡</key>
<string>101110901</string>
<key>咸阳</key>
<string>101110200</string>
<key>渭南</key>
<string>101110501</string>
<key>太原</key>
<string>101100101</string>
<key>临汾</key>
<string>101100701</string>
<key>运城</key>
<string>101100801</string>
<key>朔州</key>
<string>101100901</string>
<key>忻州</key>
<string>101101001</string>
<key>长治</key>
<string>101100501</string>
<key>大同</key>
<string>101100201</string>
<key>阳泉</key>
<string>101100301</string>
<key>晋中</key>
<string>101100401</string>
<key>晋城</key>
<string>101100601</string>
<key>吕梁</key>
<string>101101100</string>
<key>乌鲁木齐</key>
<string>101130101</string>
<key>石河子</key>
<string>101130301</string>
<key>昌吉</key>
<string>101130401</string>
<key>吐鲁番</key>
<string>101130501</string>
<key>库尔勒</key>
<string>101130601</string>
<key>阿拉尔</key>
<string>101130701</string>
<key>阿克苏</key>
<string>101130801</string>
<key>喀什</key>
<string>101130901</string>
<key>伊宁</key>
<string>101131001</string>
<key>塔城</key>
<string>101131101</string>
<key>哈密</key>
<string>101131201</string>
<key>和田</key>
<string>101131301</string>
<key>阿勒泰</key>
<string>101131401</string>
<key>阿图什</key>
<string>101131501</string>
<key>博乐</key>
<string>101131601</string>
<key>克拉玛依</key>
<string>101130201</string>
<key>拉萨</key>
<string>101140101</string>
<key>山南</key>
<string>101140301</string>
<key>阿里</key>
<string>101140701</string>
<key>昌都</key>
<string>101140501</string>
<key>那曲</key>
<string>101140601</string>
<key>日喀则</key>
<string>101140201</string>
<key>林芝</key>
<string>101140401</string>
<key>台北县</key>
<string>101340101</string>
<key>高雄</key>
<string>101340201</string>
<key>台中</key>
<string>101340401</string>
<key>海口</key>
<string>101310101</string>
<key>三亚</key>
<string>101310201</string>
<key>东方</key>
<string>101310202</string>
<key>临高</key>
<string>101310203</string>
<key>澄迈</key>
<string>101310204</string>
<key>儋州</key>
<string>101310205</string>
<key>昌江</key>
<string>101310206</string>
<key>白沙</key>
<string>101310207</string>
<key>琼中</key>
<string>101310208</string>
<key>定安</key>
<string>101310209</string>
<key>屯昌</key>
<string>101310210</string>
<key>琼海</key>
<string>101310211</string>
<key>文昌</key>
<string>101310212</string>
<key>保亭</key>
<string>101310214</string>
<key>万宁</key>
<string>101310215</string>
<key>陵水</key>
<string>101310216</string>
<key>西沙</key>
<string>101310217</string>
<key>南沙岛</key>
<string>101310220</string>
<key>乐东</key>
<string>101310221</string>
<key>五指山</key>
<string>101310222</string>
<key>琼山</key>
<string>101310102</string>
<key>长沙</key>
<string>101250101</string>
<key>株洲</key>
<string>101250301</string>
<key>衡阳</key>
<string>101250401</string>
<key>郴州</key>
<string>101250501</string>
<key>常德</key>
<string>101250601</string>
<key>益阳</key>
<string>101250700</string>
<key>娄底</key>
<string>101250801</string>
<key>邵阳</key>
<string>101250901</string>
<key>岳阳</key>
<string>101251001</string>
<key>张家界</key>
<string>101251101</string>
<key>怀化</key>
<string>101251201</string>
<key>黔阳</key>
<string>101251301</string>
<key>永州</key>
<string>101251401</string>
<key>吉首</key>
<string>101251501</string>
<key>湘潭</key>
<string>101250201</string>
<key>南京</key>
<string>101190101</string>
<key>镇江</key>
<string>101190301</string>
<key>苏州</key>
<string>101190401</string>
<key>南通</key>
<string>101190501</string>
<key>扬州</key>
<string>101190601</string>
<key>宿迁</key>
<string>101191301</string>
<key>徐州</key>
<string>101190801</string>
<key>淮安</key>
<string>101190901</string>
<key>连云港</key>
<string>101191001</string>
<key>常州</key>
<string>101191101</string>
<key>泰州</key>
<string>101191201</string>
<key>无锡</key>
<string>101190201</string>
<key>盐城</key>
<string>101190701</string>
<key>哈尔滨</key>
<string>101050101</string>
<key>牡丹江</key>
<string>101050301</string>
<key>佳木斯</key>
<string>101050401</string>
<key>绥化</key>
<string>101050501</string>
<key>黑河</key>
<string>101050601</string>
<key>双鸭山</key>
<string>101051301</string>
<key>伊春</key>
<string>101050801</string>
<key>大庆</key>
<string>101050901</string>
<key>七台河</key>
<string>101051002</string>
<key>鸡西</key>
<string>101051101</string>
<key>鹤岗</key>
<string>101051201</string>
<key>齐齐哈尔</key>
<string>101050201</string>
<key>大兴安岭</key>
<string>101050701</string>
<key>长春</key>
<string>101060101</string>
<key>延吉</key>
<string>101060301</string>
<key>四平</key>
<string>101060401</string>
<key>白山</key>
<string>101060901</string>
<key>白城</key>
<string>101060601</string>
<key>辽源</key>
<string>101060701</string>
<key>松原</key>
<string>101060801</string>
<key>吉林</key>
<string>101060201</string>
<key>通化</key>
<string>101060501</string>
<key>沈阳</key>
<string>101070101</string>
<key>鞍山</key>
<string>101070301</string>
<key>抚顺</key>
<string>101070401</string>
<key>本溪</key>
<string>101070501</string>
<key>丹东</key>
<string>101070601</string>
<key>葫芦岛</key>
<string>101071401</string>
<key>营口</key>
<string>101070801</string>
<key>阜新</key>
<string>101070901</string>
<key>辽阳</key>
<string>101071001</string>
<key>铁岭</key>
<string>101071101</string>
<key>盘锦</key>
<string>101071301</string>
<key>大连</key>
<string>101070201</string>
<key>锦州</key>
<string>101070701</string>
</dict>
</plist>
4. 删除默认的View,添加TableView,前面几篇都有介绍,下面是属性的配置
5. 添加myDataViewController类文件
5.1 修改 myDataViewController.h
2 // myDataViewController.h
3 // listCityWeather
4 //
5 // Created by tony on 12-9-6.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10 #import " NSDictionary+Json.h "
11
12 @interface myDataViewController : UITableViewController
13 {
14 // 当前的页数
15 int _index;
16
17 // 每页大小
18 int _size;
19 }
20
21 // 城市字典
22 @property NSDictionary *dictCity;
23
24 // 所有天气预报的城市
25 @property NSArray *listCity;
26
27 // 需要加载的城市
28 @property NSMutableArray *pagingCity;
29
30
31 @end
5.2 修改 myDataViewController.m
2 @synthesize dictCity;
3 @synthesize pagingCity;
4
5 - ( void)viewDidLoad
6 {
7 _index = 1; // 初始化当前页
8 _size = 10; // 设置每页行数
9
10 // 获取data.plist路径
11 NSString *path = [[NSBundle mainBundle] pathForResource: @" data " ofType: @" plist "];
12
13 // 读取文件内容到 NSDictionary
14 self.dictCity = [[NSDictionary alloc] initWithContentsOfFile:path];
15
16 // 把字典中Keys读到数组中
17 self.listCity = [[self.dictCity allKeys] sortedArrayUsingSelector:@selector(compare:)];
18
19 // 初始化显示的数组
20 self.pagingCity = [[NSMutableArray alloc] init];
21
22 // 把最前面的十个城市,存入要显示的数组中(这里没有考虑不够的情形,不能用于商业代码)
23 for ( int i= 0; i<_size; i++) {
24 [self.pagingCity addObject:[self.listCity objectAtIndex:i]];
25 }
26 [super viewDidLoad];
27 }
28
29 - ( void)viewDidUnload
30 {
31 [super viewDidUnload];
32 self.listCity = nil;
33 self.dictCity = nil;
34 self.pagingCity = nil;
35 }
2 {
3 return 1;
4 }
5
6 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
7 {
8 return [self.pagingCity count]+ 1;
9 }
10
11 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
12 {
13 static NSString *CellIdentifier = @" Cell ";
14 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
15
16 // 如果是最后一行,则显示加载更多,或是提示已加载完全
17 if([indexPath row] == ([pagingCity count])) {
18 if (indexPath.row == listCity.count)
19 {
20 cell.textLabel.text= @" 已加载所有城市! ";
21 cell.detailTextLabel.text = @" ";
22 }
23 else{
24 cell.textLabel.text= @" 加载更多.. ";
25 cell.detailTextLabel.text = @" 猛点,可以加载下面十个城市! ";
26 }
27 cell.accessoryType = UITableViewCellAccessoryNone;
28 } else {
29 // 取出城市名称
30 NSString *title = [self.listCity objectAtIndex:indexPath.row];
31
32 // 取出城城市代码
33 NSString *code = [self.dictCity objectForKey:title];
34
35 // 设置Cell参数
36 cell.textLabel.text = title;
37 cell.detailTextLabel.text = [[NSString alloc] initWithFormat: @" code: %@ ", code];
38 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
39 }
40 return cell;
41 }
2 -( void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4 if (indexPath.row == [pagingCity count])
5 {
6 NSLog( @" index path:%d ",indexPath.row);
7
8 // 自动加载,在这里没有打开,如果打开,请把_size设置20或以上,效果才能好看
9 // [self loadMoreEvent:tableView :indexPath];
10 }
11 return;
12 }
13
14 // 点击事件
15 - ( void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
16 {
17 if (indexPath.row == [pagingCity count])
18 {
19 [self loadMoreEvent:tableView :indexPath];
20 }
21 else
22 {
23 NSString *title = [self.listCity objectAtIndex:indexPath.row];
24 NSString *code = [self.dictCity objectForKey:title];
25 NSString *url = [[NSString alloc] initWithFormat: @" http://m.weather.com.cn/data/%@.html ",code];
26
27 // 把远程数据转成NSDictionary的数据
28 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURLString:url];
29
30 // 获取天气温详细信息
31 NSDictionary *weather = [dict objectForKey: @" weatherinfo "];
32 NSString *date = [weather objectForKey: @" date_y "];
33 NSString *week = [weather objectForKey: @" week "];
34 NSString *wd = [weather objectForKey: @" temp1 "];
35 NSString *tq = [weather objectForKey: @" weather1 "];
36 NSString *city = [weather objectForKey: @" city "];
37 NSString *jy = [weather objectForKey: @" index_d "];
38
39 NSString *msg = [[NSString alloc] initWithFormat: @" 城市:%@ \r 时间:%@ %@ \r 温度:%@ \r 天气:%@ \r 建议:%@ ",city,date,week,wd,tq,jy];
40
41 UIAlertView *alert= [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle: @" OK " otherButtonTitles:nil, nil];
42 [alert show];
43 }
44 }
45 // 加载更多的事件
46 -( void)loadMoreEvent:(UITableView *)tableView :(NSIndexPath *)indexPath
47 {
48 int page = ( int)(listCity.count/_size);
49 if (([self.listCity count]%_size)> 0)
50 page ++;
51 if (_index<page)
52 {
53 NSLog( @" listCity count:%d size:%d page:%d index:%d ",listCity.count,_size,page,_index);
54
55 UITableViewCell *loadMoreCell = [tableView cellForRowAtIndexPath:indexPath];
56 _index ++;
57 loadMoreCell.textLabel.text = @" 正在加载中... ";
58 loadMoreCell.detailTextLabel.text = @"";
59 [self performSelectorInBackground:@selector(loadMore) withObject:nil];
60 [tableView deselectRowAtIndexPath:indexPath animated:YES];
61 }
62 return;
63 }
64
65 // 加载更多数据,此处可以换成从远程服务器获取最新的_size条数据
66 -( void)loadMore
67 {
68 NSMutableArray *more;
69 more = [[NSMutableArray alloc] initWithCapacity: 0];
70 int begin = (_index- 1)*_size;
71 int end = begin+_size;
72 if (end>[listCity count])
73 end = [listCity count];
74
75 for ( int i=begin; i<end; i++) {
76 [more addObject:[listCity objectAtIndex:i]];
77 }
78 [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
79
80 }
81
82 // 添加数据到当前TableView中去
83 -( void) appendTableWith:(NSMutableArray *)data
84 {
85 // 添加到当前的数据源中
86 for ( int i= 0; i<[data count]; i++) {
87 [pagingCity addObject:[data objectAtIndex:i]];
88 }
89
90 NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:[data count]];
91 for( int ind = 0;ind<[data count];ind++)
92 {
93 NSIndexPath *newPath = [NSIndexPath indexPathForRow:[pagingCity indexOfObject:[data objectAtIndex:ind]] inSection: 0];
94 [insertIndexPaths addObject:newPath];
95 }
96 [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
97
98 // 选到当前的行数
99 int begin = (_index- 1)*_size;
100 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:begin inSection: 0];
101 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
102
103
6. myDataViewController 与UITableView 绑定
7. 完成,看效果
8. 按国际惯例,附上:源代码
原文地址:http://www.cnblogs.com/chinapcc/archive/2012/09/06/2663701.html#2636452