2014-07-08 98 views
2

我想顯示twitter飼料到UITableView。我能夠得到飼料和NSLog的信息。但是,我很困惑如何顯示信息而不是將其記錄到控制檯。我被告知要創建一個自定義對象,以便從Feed中存儲所需的數據,然後創建一個UITableVIew來顯示信息,那就是我卡住的地方。有沒有任何建議,或有人能指出我的方向正確嗎?這是我的代碼現在的樣子。我感謝您的寶貴時間,並感謝您的幫助。如何顯示Twitter的飼料到UITableView

CODE BELOW:

#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; 
    }; 
} 
+0

顯示我的JSON或登錄解析數據,我會幫助你在實現代碼如下 – meda

+0

謝謝顯示它!對此,我真的非常感激! – user3078406

+0

親愛的Meda,我在代碼的底部添加了它。我希望不要再給你帶來太多麻煩,謝謝! – user3078406

回答

0

聲明的類級別

numberOfSectionsInTableView

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

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; 
} 
+0

一旦我這樣做(使用我自己的鍵),我將能夠顯示信息給我的tableview?另外,我必須創建一個包含要顯示在單元中的關鍵數據的自定義對象?那是對的嗎? – user3078406

+0

是的,它會顯示數據,是的,你可以創建自定義模型類,使代碼更清潔,更強大 – meda

+0

哇!非常感謝!!!我如何爲你提供幫助? – user3078406