精华界面加载数据

  • 查看接口文档, 要求发送 get 请求, 必传参数...

    • 这里发现所有的 API 的请求接口 URL 都是同一个, 所以直接写到了常量区
    // .h 文件
    /** 请求路径 */
    UIKIT_EXTERN NSString * const CDHRequestURL;
    
    // .m 文件
    /** 请求路径 */
    NSString * const CDHRequestURL = @"http://api.budejie.com/api/api_open.php";
    
  • 使用 AFHTTPSessionManager 发送请求

    // 创建请求会话  请求参数查看接口文档
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"newlist";
    parameters[@"c"] = @"data";
    parameters[@"maxtime"] = _maxtime;// 第一次请求数据不需要这个
    parameters[@"type"] = @1;
    
    [[AFHTTPSessionManager manager] GET:CDHRequestURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
        // 保存需要加载跟多的字段
        _maxtime = responseObject[@"info"][@"maxtime"];
    
        // 写数据
        CDHAFNWriteToPlist(Essence/new_list);
    
        // 字典数组-->> 模型数组
        _topics = [CDHTopic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
    
        // 刷新表格
        [self.tableView reloadData];
    
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    }];
    
  • 接收服务器返回的数据, 写 plist 文件(将写 plist 语句写成宏 ), 查看返回数据类型, 分析模型

    // 很多地方都需要用到写数据为 plist 文件. 所以这里直接写成宏
    #define CDHAFNWriteToPlist(fileName)  [responseObject writeToFile:[NSString stringWithFormat:@"/Users/chendehao/Desktop/BuDeJie/BuDeJie/BuDeJie/Classes/%@.plist", @#fileName]  atomically:YES];
    
    // 写数据
    CDHAFNWriteToPlist(Essence/new_list);
    
    /*
    // 在宏里面, ##的作用:连接2个标识符
    #define method(name) - (void)load##name {}
    method(abc)  //- (void)loadabc {}
    method(ddd)  //- (void)loadddd {}
    method(ttt)  //- (void)loadttt {}
    
    // 在宏里面, #的作用:给右边的标识符加上双引号""
    #define test(name) @#name
    test(abc) // @"abc"
     */
    
  • 注意: 保持下一次上拉刷新请求的起始请求参数

    // 保存需要加载跟多的字段
    _maxtime = responseObject[@"info"][@"maxtime"];
    
  • 字典数组— 转— 模型数组 (MJExtion… 框架返回的就是个数组对象, 这里也就不用我们做懒加载创建数组了, 引用即可)

    _topics = [CDHTopic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
    
  • 刷新表格

    [self.tableView reloadData];
    
  • 分析5个页面的数据模型相似度较高, 这里暂时统一写在一个模型中

    /** 用户的名字 */
    @property (nonatomic, copy) NSString *name;
    /** 用户的头像 */
    @property (nonatomic, copy) NSString *profile_image;
    /** 帖子的文字内容 */
    @property (nonatomic, copy) NSString *text;
    /** 帖子审核通过的时间 */
    @property (nonatomic, copy) NSString *passtime;
    /** 顶数量 */
    @property (nonatomic, assign) NSInteger ding;
    /** 踩数量 */
    @property (nonatomic, assign) NSInteger cai;
    /** 转发\分享数量 */
    @property (nonatomic, assign) NSInteger repost;
    /** 评论数量 */
    @property (nonatomic, assign) NSInteger comment;
    

results matching ""

    No results matching ""