年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 32782|回复: 50

[新浪微博] ios实战开发之仿新浪微博(第五讲:微博数据加载)

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2014-4-17 21:09:47 | 显示全部楼层 |阅读模式
    1、效果演示



    2、设计说明

    2.1 界面设计及基本框架设计请参考前面几讲的内容
    2.2 创建数据模型,设计一个Status类用来存储从新浪API接口获取的JSON数据中微博内容,设计一个StatusUser类用来存储JSON数据中的微博用户
    2.3 创建工具模型,设计一个StatusTool类用来发送微博请求,封装HttpTool类
    2.4 优化Request请求方法,因为后续每次请求都需要在Request中添加AccessToken,因此将AccessToken自动加载到Request请求中
    2.5 调用工具模型请求微博数据,并将数据展示到首页TableView的表格中,当前TableView表格中只显示微博内容中的微博发送者昵称与微博正文,其他内容及界面展示后续完善2.6 数据请求展示示意图:
    无标题.png

    3、关键代码
    SAHttpTool.m
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    //
    //  SAHttpTool.m
    //  SianWeibo
    //
    //  Created by yusian on 14-4-15.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import "SAHttpTool.h"
    #import "AFNetWorking.h"
    #import "SAAccountTool.h"
     
    @implementation SAHttpTool
     
    // 包装第三方框架AFNetWorking,对外提供类似方法
    + (void)httpToolPostWithBaseURL:(NSString *)urlString path:(NSString *)pathString params:(NSDictionary *)params success:(Success)success failure:(Failure)failure method:(NSString *)method
    {
        // 获取授权
        // 1、创建一个Operation
        AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
         
         
        NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc] initWithDictionary:params];
         
        SAAccountTool *accountTool = [[SAAccountTool alloc] init];
         
        // 如果本在沙盒存在AccessToken则自动将AccessToken加入Request请求中
        if (accountTool.account.accessToken) {
         
            [paramsDict setObject:accountTool.account.accessToken forKey:@"access_token"];
             
        }
         
        NSURLRequest *post = [client requestWithMethod:method path:pathString parameters:paramsDict];
         
        // 2、发送Operation请求
        AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:post
                                         
            success:
            ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                 
                success(JSON);      // 将方法中的实参传给内部的Success Block调用
            }
                                         
            failure:
            ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                 
                failure(error);     // 将方法中的error传给内部的Failure Block调用
            }];
         
        [json start];
    }
     
    @end

    SAStatus.h
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    //
    //  SAStatus.h
    //  SianWeibo
    //
    //  Created by yusian on 14-4-16.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import <Foundation/Foundation.h>
    #import "SAStatusUser.h"
     
    @interface SAStatus : NSObject
     
    @property (nonatomic, copy) NSString        *text;
    @property (nonatomic, strong) SAStatusUser  *user;
     
    - (id)initWithDict:(NSDictionary *)dict;
     
    + (id)statusWithDict:(NSDictionary *)dict;
     
    @end

    SAStatus.m
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    //
    //  SAStatus.m
    //  SianWeibo
    //
    //  Created by yusian on 14-4-16.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import "SAStatus.h"
     
    @implementation SAStatus
     
    - (id)initWithDict:(NSDictionary *)dict
    {
        if (self = [super init]) {
            _text = dict[@"text"];
            _user = [SAStatusUser statusUserWithDict:dict[@"user"]];
        }
        return self;
    }
     
    + (id)statusWithDict:(NSDictionary *)dict
    {
        return [[self alloc] initWithDict:dict];
    }
     
    @end

    SAStatusUser.h
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //
    //  SAStatusUser.h
    //  SianWeibo
    //
    //  Created by yusian on 14-4-16.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import <Foundation/Foundation.h>
     
    @interface SAStatusUser : NSObject
     
    @property (nonatomic, copy) NSString *screenName;
     
    - (id)initWithDict:(NSDictionary *)dict;
     
    + (id)statusUserWithDict:(NSDictionary *)dict;
     
    @end

    SAStatusUser.m
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    //
    //  SAStatusUser.m
    //  SianWeibo
    //
    //  Created by yusian on 14-4-16.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import "SAStatusUser.h"
     
    @implementation SAStatusUser
     
    - (id)initWithDict:(NSDictionary *)dict
    {
        if (self = [super init]) {
            _screenName = dict[@"screen_name"];
        }
        return self;
    }
     
    + (id)statusUserWithDict:(NSDictionary *)dict
    {
        return [[self alloc] initWithDict:dict];
    }
     
    @end

    SAStatusTool.h
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    //
    //  SAStatusTool.h
    //  SianWeibo
    //
    //  Created by yusian on 14-4-16.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import <Foundation/Foundation.h>
     
    typedef void(^StatusSuccess)(NSArray * array);
    typedef void(^StatusFailurs)(NSError * error);
     
    @interface SAStatusTool : NSObject
     
    - (void)initWithHttpToolStatusSuccess:(StatusSuccess)success failurs:(StatusFailurs)failure;
     
    + (void)statusToolGetStatusSuccess:(StatusSuccess)success failurs:(StatusFailurs)failure;
     
    @end

    SAStatusTool.m
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    //
    //  SAStatusTool.m
    //  SianWeibo
    //
    //  Created by yusian on 14-4-16.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //
     
    #import "SAStatusTool.h"
    #import "SAStatus.h"
    #import "SAHttpTool.h"
     
    @implementation SAStatusTool
     
    + (void)statusToolGetStatusSuccess:(StatusSuccess)success failurs:(StatusFailurs)failure
    {
        [[self alloc] initWithHttpToolStatusSuccess:success failurs:failure];
    }
     
    - (void)initWithHttpToolStatusSuccess:(StatusSuccess)success failurs:(StatusFailurs)failure
    {
        // 调用SAHttpTool这个工具类,发送相关请求返回数组内容
        [SAHttpTool httpToolPostWithBaseURL:kBaseURL path:@"2/statuses/home_timeline.json" params:
         nil success:^(id JSON) {
              
             // 如果方法调用没有实现success部分,则方法直接返回
             if (success == nil) return;
              
             // 1、将返回的JSON转换成微博模型并保存到数组
             NSMutableArray *statuses = [NSMutableArray array];
             for (NSDictionary *dict in JSON[@"statuses"]) {
                 SAStatus *status = [SAStatus statusWithDict:dict];
                 [statuses addObject:status];
             }
              
             // 2、将数组返回给Block形参供方法调用者使用
             success(statuses);
              
         } failure:^(NSError *error) {
              
             if (failure == nil) return;
              
             failure(error);
              
         } method:@"GET"];
         
    }
    @end

    SAHomeController.m
    [Objective-C] 纯文本查看 复制代码
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    //
    //  SAHomeController.m
    //  SianWeibo
    //
    //  Created by yusian on 14-4-12.
    //  Copyright (c) 2014年 小龙虾论坛. All rights reserved.
    //  首页控制器
     
    #import "SAHomeController.h"
    #import "NSString+SA.h"
    #import "UIBarButtonItem+SA.h"
    #import "SAStatus.h"
    #import "SAStatusTool.h"
     
     
    @interface SAHomeController ()
    {
        NSArray *_status;
    }
     
    @end
     
    @implementation SAHomeController
     
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }
     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        _status = [NSMutableArray array];
         
        // 1、设置基本界面
        [self loadBasicUI];
         
        // 2、加载数据
         
        [self loadStatusData];
         
    }
     
    #pragma mark 加载基本界面
    - (void)loadBasicUI
    {
         
        self.title = @"首页";
         
        // 用自定的分类方法给导航条添加左边按钮
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"navigationbar_compose.png" highLightedImageName:@"navigationbar_compose_highlighted.png" addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
         
        // 用自定的分类方法给导航条添加右边按钮
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"navigationbar_pop.png" highLightedImageName:@"navigationbar_pop_highlighted.png" addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
         
    }
     
    #pragma mark 加载微博数据
    - (void)loadStatusData
    {
        // 调用SAStatusTool方法直接加载数据到模型数组
        [SAStatusTool statusToolGetStatusSuccess:^(NSArray *array) {
             
            _status = [NSArray arrayWithArray:array];
            [self.tableView reloadData];
             
        } failurs:^(NSError *error) {
             
            MyLog(@"%@", [error localizedDescription]);
             
        }];
         
    }
     
    #pragma mark 首页导航左按钮事件
    - (void)leftButtonClick
    {
        MyLog(@"首页左按钮");
    }
     
    #pragma mark 首页导航右按钮事件
    - (void)rightButtonClick
    {
        MyLog(@"首页右按钮");
    }
     
    #pragma mark - Table view data source
     
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //    MyLog(@"%d", _status.count);
        return _status.count;
    }
     
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *Identifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier];
        }
        // 使用数据模型取内容
        SAStatus *status = _status[indexPath.row];
        cell.textLabel.text = status.user.screenName;
        cell.detailTextLabel.text = status.text;
         
        return cell;
    }
     
    @end


    4、源码下载
    游客,如果您要查看本帖隐藏内容请回复


    相关主题链接
    1、ios实战开发之仿新浪微博(第一讲:新特性展示)
    2、ios实战开发之仿新浪微博(第二讲:主框架搭建)
    3、ios实战开发之仿新浪微博(第三讲:更多界面搭建)
    4、ios实战开发之仿新浪微博(第四讲:OAuth认证)
    5、ios实战开发之仿新浪微博(第五讲:微博数据加载)
    6、ios实战开发之仿新浪微博(第六讲:微博数据展示一)
    7、ios实战开发之仿新浪微博(第七讲:微博数据展示二)
    8、ios实战开发之仿新浪微博(第八讲:微博数据展示三)
    9、ios实战开发之仿新浪微博(第九讲:微博功能完善一)
    10、ios实战开发之仿新浪微博(第十讲:微博功能完善二)
    11、ios实战开发之仿新浪微博(第十一讲:微博功能完善三)
    12、ios实战开发之仿新浪微博(小龙虾发布版)

    该用户从未签到

    发表于 2014-10-24 16:00:56 | 显示全部楼层
    继续感谢!!!!!!
    回复

    使用道具 举报

    该用户从未签到

    发表于 2014-11-4 13:11:56 | 显示全部楼层
    嗯,不错,学习中...
  • TA的每日心情
    发光
    2014-11-26 14:19
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2014-11-20 20:33:08 | 显示全部楼层
    、、、、、、、、、、、、、、

    该用户从未签到

    发表于 2014-11-25 13:22:02 | 显示全部楼层
    Oh,已经铺不急待
  • TA的每日心情

    2014-12-29 14:23
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2014-11-28 09:49:41 | 显示全部楼层
    学习怎么加载数据中……!!!
  • TA的每日心情

    2014-12-10 10:56
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2014-11-28 12:41:49 | 显示全部楼层
    学习学习:)
    回复

    使用道具 举报

    该用户从未签到

    发表于 2014-11-30 09:41:11 | 显示全部楼层
    学习加载数据~~~~
  • TA的每日心情
    得瑟
    2015-1-8 17:35
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2014-12-20 20:00:44 | 显示全部楼层
    支持个,学习学习

    该用户从未签到

    发表于 2014-12-24 09:15:01 | 显示全部楼层
    楼主今天心情很好啊
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2025-4-26 18:47 , Processed in 0.054498 second(s), 24 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表