下面是一个AFNetworking使用的小例子:
1.创建FKBooksController类 : UITableViewController
#import <UIKit/UIKit.h>
@interface FKBooksController : UITableViewController
@property (nonatomic, strong) NSDictionary* selectedAuthor;
@end
#import "FKBooksController.h"
#import "FKAppDelegate.h"
@interface FKBooksController ()
{
NSArray* books;
FKAppDelegate* appDelegate;
}
@end
@implementation FKBooksController
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [UIApplication sharedApplication].delegate;
NSString* url = @"http://192.168.1.88:8888/AFNetworkingServer/books.json";
self.navigationItem.title = [NSString stringWithFormat:@"%@的图书"
, [self.selectedAuthor objectForKey:@"name"]];
// 使用NSDictionary封装请求参数
NSDictionary *parameters = @{@"authorId":
[self.selectedAuthor objectForKey:@"id"]};
// 使用AFHTTPRequestOperationManager发送GET请求
[appDelegate.manager POST:url parameters:parameters
// 获取服务器响应成功时激发的代码块
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
// 将服务器响应的JSON数据转换为Objective-C对象,赋值给books属性
books = responseObject;
// 重新加载表格数据
[self.tableView reloadData];
}
// 获取服务器响应失败时激发的代码块
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"获取图书信息出现错误: %@", error);
}];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return books.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 根据动态单元格原型的ID来获取可重用单元格
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
@"bookCell" forIndexPath:indexPath];
// 获取当前行号在books中对应的数据
NSDictionary* book = [books objectAtIndex:indexPath.row];
// 获取单元格中3个控件,并为3个控件设置显示文本
UILabel* titleLable = (UILabel*)[cell viewWithTag:1];
titleLable.text = [book objectForKey:@"title"];
UILabel* authorLable = (UILabel*)[cell viewWithTag:2];
authorLable.text = [book objectForKey:@"author"];
UILabel* remarkLabel = (UILabel*)[cell viewWithTag:3];
remarkLabel.text = [book objectForKey:@"remark"];
return cell;
}
@end
2.创建FKAuthorsController类 : UITableViewController,导入FKBooksController和FKAppDelegate
#import "FKAuthorsController.h"
#import "FKBooksController.h"
#import "FKAppDelegate.h"
@interface FKAuthorsController ()
{
NSArray* authors;
FKAppDelegate* appDelegate;
}
@end
@implementation FKAuthorsController
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [UIApplication sharedApplication].delegate;
NSString* url = @"http://192.168.1.88:8888/AFNetworkingServer/authors.json";
// 使用AFHTTPRequestOperationManager发送GET请求
[appDelegate.manager GET:url parameters:nil
// 获取服务器响应成功时激发的代码块
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
// 将服务器响应的JSON数据转换为Objective-C对象,赋值给authors属性
authors = responseObject;
// 重新加载表格数据
[self.tableView reloadData];
}
// 获取服务器响应失败时激发的代码块
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"获取作者信息出现错误: %@", error);
}];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewCell* cell = (UITableViewCell*)sender;
// 获取激发跳转的单元格所在的NSIndexPath
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
// 获取即将跳转到的目标视图控制器
FKBooksController* booksController = (FKBooksController*)
segue.destinationViewController;
// 将用户选中的单元格的作者信息传给目标视图控制器
booksController.selectedAuthor = [authors objectAtIndex:indexPath.row];
}
// --------下面两个方法根据authors集合的元素来显示表格数据---------
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return authors.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
@"authorCell" forIndexPath:indexPath];
NSDictionary* dict = [authors objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:@"name"];
cell.detailTextLabel.text = [dict objectForKey:@"desc"];
return cell;
}
@end