网络请求 下拉刷新 block传值
来源:互联网 发布:床垫 知乎 编辑:程序博客网 时间:2024/05/09 05:51
//// RootTableViewController.m// NetJSONCountry//// Created by sunlihuo on 15/5/26.// Copyright (c) 2015年 sunlihuo. All rights reserved.//#import "RootTableViewController.h"#import "Country.h"#import "CityTableViewController.h"#import "Common.h"@interface RootTableViewController ()@property (strong, nonatomic) NSMutableArray *countrys;@property (strong, nonatomic) CityTableViewController *cityVC;@end@implementation RootTableViewController- (instancetype)init{ self = [super init]; if (self) { self.countrys = [NSMutableArray new]; [self onCreate]; } return self;}-(void)onCreate{ NSURL *url = [NSURL URLWithString:kWeiboCountryURL]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError) { NSLog(@"error is %@", connectionError); } else { NSError *error = nil; NSArray *tmpArr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; //NSLog(@"tmpDict is %@", tmpArr); NSDictionary *tmpDict = nil; Country *country = nil; for (int i = 0; i < tmpArr.count; i++) { tmpDict = tmpArr[i]; for (NSString *key in tmpDict.allKeys) { country = [Country new]; country.countryID = key; country.countryName = tmpDict[key]; [self.countrys addObject:country]; } } //NSLog(@"self.countrys is %@", self.countrys); dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); } }]; }-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; UIBarButtonItem *reloadBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(reload:)]; reloadBtn.title = @"刷新"; [self.navigationItem setRightBarButtonItem:reloadBtn animated:YES]; }-(void)reload:(UIBarButtonItem *)sender{ [self onCreate]; }- (void)viewDidLoad { [super viewDidLoad]; //创建下拉刷新组件 self.refreshControl = [[UIRefreshControl alloc]init]; self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"松开刷新"]; [self.refreshControl addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged]; }#pragma mark 下拉刷新-(void)refreshTableView{ [self.refreshControl beginRefreshing]; [self onCreate]; [self.refreshControl endRefreshing];}- (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 { //NSLog(@"self.countrys.count is %zi", self.countrys.count); return self.countrys.count; //return 10;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellID = @"cellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } //NSLog(@"cell is %@", cell); Country *country = self.countrys[indexPath.row]; cell.textLabel.text = country.countryName; cell.detailTextLabel.text = country.countryID; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //NSLog(@"country.countryName is %@", country.countryName); return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (!self.cityVC) { self.cityVC = [CityTableViewController new]; } self.cityVC.title = [NSString stringWithFormat:@"%@城市列表",self.countrys[indexPath.row]]; self.cityVC.country = self.countrys[indexPath.row]; [self.cityVC initWithBlock:^(Country *country) { NSString *msg = [NSString stringWithFormat:@"您选择了%@", country]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; }]; [self.navigationController pushViewController:self.cityVC animated:YES]; }/*// 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
//// CityTableViewController.h// NetJSONCountry//// Created by sunlihuo on 15/5/26.// Copyright (c) 2015年 sunlihuo. All rights reserved.//#import <UIKit/UIKit.h>#import "Country.h"#import "Country.h"typedef void(^MyBlock)(Country *country);@interface CityTableViewController : UITableViewController@property (strong, nonatomic) Country *country;@property (strong, nonatomic) MyBlock myBlock;-(void)initWithBlock:(MyBlock)block;@end
//// CityTableViewController.m// NetJSONCountry//// Created by sunlihuo on 15/5/26.// Copyright (c) 2015年 sunlihuo. All rights reserved.//#import "CityTableViewController.h"#import "RootTableViewController.h"@interface CityTableViewController ()@property (strong, nonatomic) NSMutableArray *citys;@end@implementation CityTableViewController- (instancetype)init{ self = [super init]; if (self) { } return self;}- (void)viewDidLoad { [super viewDidLoad]; }- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self onCreate]; }-(void)onCreate{ self.citys = [NSMutableArray new]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/2/common/get_province.json?access_token=2.00ZtlF5C0GvJSM28ba1329cb6CdARD&country=%@",self.country.countryID]]; NSLog(@"%@", self.country.countryID); NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError) { NSLog(@"error is %@", connectionError); } else { NSError *error = nil; NSArray *tmpArr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"tmpDict count is %zi", tmpArr.count); NSDictionary *tmpDict = nil; Country *country = nil; for (int i = 0; i < tmpArr.count; i++) { tmpDict = tmpArr[i]; for (NSString *key in tmpDict.allKeys) { country = [Country new]; country.countryID = key; country.countryName = tmpDict[key]; [self.citys addObject:country]; } } NSLog(@"self.citys is %@", self.citys); dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); } }]; }- (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.citys.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"**************************"); NSString *cellID = @"cityCellID"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } //NSLog(@"cell is %@", cell); Country *country = self.citys[indexPath.row]; cell.textLabel.text = country.countryName; cell.detailTextLabel.text = country.countryID; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; NSLog(@"country.countryName is %@", country.countryName); return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.navigationController popViewControllerAnimated:YES]; self.myBlock(self.citys[indexPath.row]);}-(void)initWithBlock:(MyBlock)block{ self.myBlock = block;}/*// 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
0 0
- 网络请求 下拉刷新 block传值
- IOS网络请求封装与下拉刷新上托加载更多
- 使用AsyncTask 实现网络请求及PullToRefreshListView下拉刷新
- XListView上拉刷新下拉加载(网络请求json)
- HttpUtils请求网络+xlistview上拉刷新,下拉加载
- xutils3网络请求结合下拉刷新事件处理缓存
- 异步block网络请求
- iOS开发用AFNetworking和MJRefresh实现网络请求和下拉刷新、上拉加载
- Swift网络请求,数据模型,图片缓存,自动布局,上拉刷新下拉加载
- xListView(上拉刷新下拉加载,xml解析)中添加网络请求
- PullToRefreshListView 上拉加载,下拉刷新 + AsyncTask 网络请求 + gson 自动解析
- ReactNative 项目实战 (2) ListView 使用 网络请求以及下拉刷新 (条目控件封装)
- MVP模式的OKhttp请求网络数据,xrecyclerview上拉刷新,下拉加载
- MVP请求网络数据上拉刷新下拉加载加断点续传
- fresco加载图片+retrofit网络请求+rxjava2+xrecyclerview下拉刷新上拉加载
- xlistview+imageloder+数据请求+下拉刷新
- XListView 刷新 + AsyncHttpClient网络请求
- HttpConnection网络请求,pullToRefreshListView刷新
- js变量作用域和内存问题
- 根据公历时间计算农历时间
- 北恩科技与Mentor(明导科技)联合研讨会---信号/电源完整性仿真技术在PCB设计中的应用研讨会
- CSS图片廊实例详解
- C#中抽象类与接口的区别之处
- 网络请求 下拉刷新 block传值
- 字符数组与字符指针
- => 符号
- 重构方法之处理概括关系(继承关系)
- 理解GL_TRIANGLE_STRIP(译)
- stringstream的基本用法
- Java基础(极客)——04、Java语言逻辑运算
- [Mybatis + Oracle] 格式化时间
- 文件操作