将twitter搜索添加到tableview中的自定义单元格

时间:2012-06-09 12:43:22

标签: objective-c uitableview twitter nsdictionary custom-cell

我有一个TWRequest创建了一个名为dict的字典输出。我想将结果输出到tableView中的自定义单元格中。诀窍是每个推文被分割(因为它的'文本'将被分成由冒号分隔的部分)并放入一个数组我正在努力解决如何从字典中取出'text'组件并应用它到tableview。这是我的代码...任何建议将不胜感激。提前致谢。

    - (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;
    }

0 个答案:

没有答案