Question

Je suis en Xcode 4.2 en utilisant Arc et Storyboard (iOS 5).Je suppose de mettre des tweets dans ma tableViewController.Comme je me suis placé ma Nsurlconnection, tout va bien et j'ai déjà la réponse dans Tweets (Nsarray) dans le délégué.Le problème est qu'il ne remplit pas la vue de la table.J'utilise SBJSON pour les données reçues.Est-ce que je manque quelque chose?

appdelegate.m

#import "AppDelegate.h"
#import "TwitterViewController.h"
#import "SBJson.h"


@implementation AppDelegate

@synthesize window = _window;

@synthesize receivedData;
@synthesize tableViewController;
@synthesize tweets;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    tweets = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"http://search.twitter.com/search.json?q={username}&rpp=5"]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    if (theConnection) {
        receivedData = [NSMutableData data];
    }
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{

    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);


    NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSDictionary *results = [responseString JSONValue];

    NSArray *allTweets = [results objectForKey:@"results"];

    [tableViewController setTweets:allTweets];
}
@end

Il s'agit des méthodes déléguées de mon contrôleur de vue de la table.

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

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

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

    NSDictionary *aTweet = [tweets objectAtIndex:[indexPath row]];



    NSString *textFrom = [aTweet objectForKey:@"text"];
    cell.textLabel.text = textFrom;


    return cell;
}

J'ai vraiment besoin de conseils.

Était-ce utile?

La solution

Table reload. Gosh! I learned!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top