0

I have a TWRequest that has created a dictionary output called dict. I would like to output the results of this into a custom cell in a tableView. The trick bit is that each individual tweet is then split (as its 'text' will be split into sections separated by colons) and put into an Array I am struggling with how to take the 'text' component out of the dictionary and apply this to the tableview. Here is my code... Any advise would be greatly appreciated. Thanks in advance.

    - (void)fetchTweets
{
    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         @"http://search.twitter.com/search.json?q=myhashtagforsearchgoeshere"] 
                                             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]);
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"MyCell";
        // I'm using a custom cell
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



        // then I want to grab the text part only from the tweets
        NSString *tweetText = [[dict objectForKey:@"text"]objectAtIndex:indexPath.row];  
        // then I place the output into an array (the tweet is carved up into 3 components separated by a colon
    NSArray *tweetComponents = [tweetText componentsSeparatedByString:@":"]; 
                    NSLog(@"Tweettext: %@", tweetText);  

    // then I link this to my labels in my custom cell using objectatindex
        cell.myHeader.text = [tweetComponents objectAtIndex:0];
        cell.myDetails.text = [tweetComponents objectAtIndex:1];
        cell.myDate.text = [tweetComponents objectAtIndex:2];

        return cell;
    }
Alan
  • 63
  • 7

0 Answers0