2012-03-12 66 views
0

我现在试图做的是获得我的json信息(twitter用户时间轴)并将其粘贴到一张表中,关于它的所有内容json的作品,但是当涉及到将信息添加到的tableView它不会做任何事情,这是我的代码:把我的JSON放到一个tableView中

#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 2012-03-12 17:40:08

+1

不,我只是一个白痴,只注意到, – 2012-03-12 17:59:05

回答

2

您在您的fetchData执行结束无缘reloadData

+0

我试着添加“[self.tableView reloadData];”但因为tableView是在视图控制器内它不接受tableView任何帮助,除此之外,那你的位置 – 2012-03-12 17:58:29

+0

' [((UITableView *)self.view reloadData];' – Till 2012-03-12 18:03:40

+0

那工作,谢谢你! – 2012-03-12 18:17:28

4

您必须至少返回一个部分到表格。现在你是返回零部分。

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

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

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

你的权利,我没有看到,但仍然没有出现这是奇怪的 – 2012-03-12 17:35:06

+0

+1正如reloadData省略一样重要。 – Till 2012-03-13 14:10:23

相关问题