0

I am trying to propagate a table view with a custom cell with the results of a TWRequest with no luck. I am trying to store the tweets in a dictionary object then calling an ObjectAtKey request and putting into a string. Here is my code - any help would be appreciated. Thanks -

-(void)fetchTweets
{
    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         @"http://search.twitter.com/search.json?q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"] 
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
    {
        if ([urlResponse statusCode] == 200) 
        {
            // The response from Twitter is in JSON format
            // Move the response into a dictionary and print
            NSError *error;        
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
            NSLog(@"Twitter response: %@", dict);                           
        }
        else
            NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
    }];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }  


    NSString *tweetText = [dict objectForKey:@"text"];
    NSArray *tweetComponents = [tweetText componentsSeparatedByString:@":"]; 


    cell.Header.text = [tweetComponents objectAtIndex:0];
    cell.Details.text = [tweetComponents objectAtIndex:1];
    cell.Date.text = [tweetComponents objectAtIndex:2];

    return cell;
}
4

1 回答 1

1

您将需要从 Twitter 响应中创建推文的 NSMutableArray。每条推文都是一个单独的对象。

NSMutableArray *resultArray = [[NSMutableArray] alloc] init];
for (NSDictionary *tweet in dict)
{
     [resultArray addObject:tweet];
}

然后cellForRowAtIndexPath根据表的行检索对象,例如

TweetResult *tweetResult [resultArray objectAtIndex:[indexPath row]];

于 2012-06-26T00:28:29.737 回答