将我的JSON放入tableView

时间:2012-03-12 17:27:29

标签: iphone ios json uitableview

我目前正在尝试做的是获取我的json信息(twitter用户时间轴)并将其粘贴到一个表中,关于它的所有内容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

但它永远不会将数据加载到表视图中,有什么帮助吗?

旁注:

我在界面构建器中使用它。

&安培;理想情况下,我想制作一个自定义单元格,这样我就可以计算每个单元格的布局,所以如果你知道任何好的网站会展示如何做到这一点,那也是一个大手。

2 个答案:

答案 0 :(得分:4)

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

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

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

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

答案 1 :(得分:2)

您在reloadData实施结束时错过了fetchData