// XMGSubTagViewController.m #import "XMGSubTagViewController.h" #import <AFNetworking/AFNetworking.h> #import "XMGSubTagItem.h" #import <MJExtension/MJExtension.h> @interface XMGSubTagViewController () @property (nonatomic, strong) NSArray *subTags; @end @implementation XMGSubTagViewController // 接口文档: 请求url(基本url+请求参数) 请求方式 - (void)viewDidLoad { [super viewDidLoad]; // 展现标签数据 -> 请求数据(接口文档) -> 解析数据(写成Plist)(image_list,sub_number,theme_name) -> 设计模型 -> 字典转模型 -> 展现数据 [self loadData]; } #pragma mark - 请求数据 - (void)loadData { // 1.建立请求会话管理者 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; // 2.拼接参数 NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; parameters[@"a"] = @"tag_recommend"; parameters[@"action"] = @"sub"; parameters[@"c"] = @"topic"; // 3.发送请求 [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSArray * _Nullable responseObject) { // [responseObject writeToFile:@"/Users/xiaomage/Desktop/课堂共享/11大神班上课资料/08-项目/0315/代码/05-订阅标签/tag.plist" atomically:YES]; // NSLog(@"%@",responseObject); // 字典数组转换模型数组 _subTags = [XMGSubTagItem mj_objectArrayWithKeyValuesArray:responseObject]; // 刷新表格 [self.tableView reloadData]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.subTags.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; // 自定义cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 获取模型 XMGSubTagItem *item = self.subTags[indexPath.row]; cell.textLabel.text = item.theme_name; return cell; } @end