2012-03-12 2 views
0

現在私がしようとしていることは、自分のjson情報(twitterユーザーのタイムライン)を取得してテーブルに貼り付けることです.Jsonについては何でも構いませんが、それは何もしないtableViewそれは私のコードです:JSONをテーブルビューにする

#import "FirstViewController.h" 
#import <Twitter/Twitter.h> 
#import <Accounts/Accounts.h> 

@interface FirstViewController() 
- (void)fetchData; 
@end 

@implementation FirstViewController 
@synthesize timelineTwiter = _timelineTwiter; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"First", @"First"); 
     self.tabBarItem.image = [UIImage imageNamed:@"first"]; 
    } 
    return self; 
} 

-(void)fetchData { 
    ACAccountStore *store = [[ACAccountStore alloc] init]; 
    ACAccountType *twitterAccountType = 
    [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    [store requestAccessToAccountsWithType:twitterAccountType 
        withCompletionHandler:^(BOOL granted, NSError *error) { 
         if (!granted) { 
          NSLog(@"User rejected access to his account."); 
         } 
         else { 
          NSArray *twitterAccounts = 
          [store accountsWithAccountType:twitterAccountType]; 

          if ([twitterAccounts count] > 0) { 
           ACAccount *account = [twitterAccounts objectAtIndex:0]; 

           NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
           [params setObject:@"1" forKey:@"include_entities"]; 

           NSURL *url = 
           [NSURL 
            URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"]; 

           TWRequest *request = 
           [[TWRequest alloc] initWithURL:url 
                parameters:params 
                requestMethod:TWRequestMethodGET]; 

           [request setAccount:account]; 

           [request performRequestWithHandler: 
            ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
             if (!responseData) { 
              NSLog(@"%@", error); 
             } 
             else { 
              NSError *jsonError; 
              NSArray *timeline = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError]; 
              self.timelineTwiter = timeline; 
              if (timeline) {       
               NSDictionary* tweets0 = [timeline objectAtIndex:0]; 
               NSLog(@"%@", [tweets0 objectForKey:@"text"]); 
               NSLog(@"%@", [[tweets0 objectForKey:@"user"] objectForKey:@"screen_name"]); 
               NSDictionary* tweets1 = [timeline objectAtIndex:1]; 
               NSLog(@"%@", [tweets1 objectForKey:@"text"]); 
               NSLog(@"%@", [[tweets1 objectForKey:@"user"] objectForKey:@"screen_name"]); 
               NSDictionary* tweets2 = [timeline objectAtIndex:2]; 
               NSLog(@"%@", [tweets2 objectForKey:@"text"]); 
               NSLog(@"%@", [[tweets2 objectForKey:@"user"] objectForKey:@"screen_name"]); 


              } 
              else { 
               NSLog(@"%@", jsonError); 
              } 
             } 
            }]; 

          } 
         } 
        }]; 

} 

-(IBAction)refreshTimeline:(id)sender { 
    [self fetchData]; 
} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.timelineTwiter count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    id userTimeline = [self.timelineTwiter objectAtIndex:[indexPath row]]; 
    cell.textLabel.text = [userTimeline objectForKey:@"text"]; 
    cell.detailTextLabel.text = [userTimeline valueForKeyPath:@"user.name"]; 


    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

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

- (void)viewWillAppear:(BOOL)animated 
{ 
    [self fetchData]; 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

しかし、それは決してテーブルビュー、任意のヘルプにデータを読み込みますか?

サイドノート:

私はインターフェイスビルダーで使用しています。

&理想的には、カスタムセルを作成して各セルのレイアウトを調整できるようにしたいので、それを行う方法を示す良いサイトを知っていれば、それも大きな手になります。

+0

「timelineTwiter」に「t」を意図的に残しましたか? – Andrew

+1

いいえ私はばかだとただ気がついただけです –

答えて

2

あなたfetchData実装の終わりにreloadDataを逃しました。

+0

"[self.tableView reloadData];を追加しようとしました。テーブルビューはビューコントローラ内にあるので、tableViewを受け入れていないので、それ以外のヘルプは –

+0

'((UITableView *)self.view reloadData]; – Till

+0

にあります。 –

4

少なくとも1つのセクションをテーブルに少なくとも戻す必要があります。今ではゼロを返すセクションです。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 0; 
} 

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

あなたの権利は、私はそれを見ませんでしたが、まだ何も表示されていません –

+0

+1 reloadDataの省略と同じくらい重要です。 – Till

関連する問題