如何将Twitter Feed显示到UITableView中

时间:2014-07-08 18:56:06

标签: xcode uitableview

我正在尝试将Twitter Feed显示到UITableView中。我能够获取feed和NSLog的信息。但是,我对如何显示信息而不是将其记录到控制台感到困惑。有人告诉我创建一个自定义对象,以便从feed中存储所需的数据,然后创建一个UITableVIew来显示信息,这就是我被卡住的地方。是否有任何建议或任何人都指出我正确的方向?以下是我的代码现在的样子。我感谢您的帮助,感谢您宝贵的时间。

以下代码:

#import "ViewController.h"
#import <Accounts/Accounts.h>
#import <Social/Social.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [self refreshTwitter];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




    -(void)refreshTwitter
    {
        ACAccountStore *accountStore = [[ACAccountStore alloc]init];

        if (accountStore != nil)
        {
            ACAccountType *accountType = [accountStore     accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
            if (accountType != nil)
            {
                [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
                 {
                     if (granted)
                     {
                         //Succesful Access
                         NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
                         if (twitterAccounts != nil)
                         {
                             ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
                             if (currentAccount != nil)
                             {
                                 NSString *requestString = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
                                 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString] parameters:nil];
                                 [request setAccount:currentAccount];

                                 [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                                 {
                                     if ((error == nil) && ([urlResponse statusCode] == 200))
                                     {
                                         NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                                         NSDictionary *firstPost = [twitterFeed objectAtIndex:0];

                                         NSLog(@"firstPost = %@", [firstPost description]);
                                     }
                                 }];

                             }
                         }
                     }
                     else
                     {
                          //Access Denied
                     }
                 }];
            }
        }

    }

    @end

JSON DATA:

2014-07-08 13:22:37.442 demoApp[19277:4607] firstPost = {
    contributors = "<null>";
    coordinates = "<null>";
    "created_at" = "Wed Jul 02 18:29:43 +0000 2014";
    entities =     {
        hashtags =         (
                        {
                indices =                 (
                    0,
                    20
                );
                text = ikercasillasoficial;
            }
        );
        symbols =         (
        );
        urls =         (
        );
        "user_mentions" =         (
        );
    };
    "favorite_count" = 0;
    favorited = 0;
    geo = "<null>";
    id = 484403559677837312;
    "id_str" = 484403559677837312;
    "in_reply_to_screen_name" = "<null>";
    "in_reply_to_status_id" = "<null>";
    "in_reply_to_status_id_str" = "<null>";
    "in_reply_to_user_id" = "<null>";
    "in_reply_to_user_id_str" = "<null>";
    lang = es;
    place = "<null>";
    "retweet_count" = 0;
    retweeted = 0;
    source = "<a href=\"http://twitter.com/#!/download/ipad\" rel=\"nofollow\">Twitter for iPad</a>";
    text = "#ikercasillasoficial Bien Iker. Pero fuiste muy sutil. Ojala ese hp cuando tenga hijos se le pudran en el vientre se su mujer.";
    truncated = 0;
    user =     {
        "contributors_enabled" = 0;
        "created_at" = "Wed Jan 18 02:10:12 +0000 2012";
        "default_profile" = 1;
        "default_profile_image" = 0;
        description = "Hello world";
        entities =         {
            description =             {
                urls =                 (
                );
            };
        };
        "favourites_count" = 0;
        "follow_request_sent" = 0;
        "followers_count" = 2;
        following = 0;
        "friends_count" = 18;
        "geo_enabled" = 0;
        id = 467043064;
        "id_str" = 467043064;
        "is_translation_enabled" = 0;
        "is_translator" = 0;
        lang = en;
        "listed_count" = 0;
        location = "";
        name = "Omar Devila";
        notifications = 0;
        "profile_background_color" = C0DEED;
        "profile_background_image_url" = "http://abs.twimg.com/images/themes/theme1/bg.png";
        "profile_background_image_url_https" = "https://abs.twimg.com/images/themes/theme1/bg.png";
        "profile_background_tile" = 0;
        "profile_image_url" = "http://pbs.twimg.com/profile_images/483760718895140864/3pLRpyzk_normal.jpeg";
        "profile_image_url_https" = "https://pbs.twimg.com/profile_images/483760718895140864/3pLRpyzk_normal.jpeg";
        "profile_link_color" = 0084B4;
        "profile_sidebar_border_color" = C0DEED;
        "profile_sidebar_fill_color" = DDEEF6;
        "profile_text_color" = 333333;
        "profile_use_background_image" = 1;
        protected = 0;
        "screen_name" = DevilaOmar;
        "statuses_count" = 18;
        "time_zone" = "<null>";
        url = "<null>";
        "utc_offset" = "<null>";
        verified = 0;
    };
}

1 个答案:

答案 0 :(得分:0)

在类级别声明twitterFeed数组

<强> numberOfSectionsInTableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

<强> numberOfRowsInSection

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return twitterFeed.count;
}

<强>的cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel = twitterFeed[indexPath.row][@"user"][@"screen_name"];
    return cell;
}