首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tableView单元格中字典中的TWRequest

tableView单元格中字典中的TWRequest
EN

Stack Overflow用户
提问于 2012-06-07 00:20:25
回答 1查看 426关注 0票数 0

我正在尝试传播一个带有自定义单元格的表视图,该单元格包含TWRequest的结果,但没有成功。我尝试将tweet存储在一个字典对象中,然后调用一个ObjectAtKey请求并放入一个字符串中。这是我的代码-任何帮助都将不胜感激。谢谢-

代码语言:javascript
复制
-(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;
}
EN

回答 1

Stack Overflow用户

发布于 2012-06-26 08:28:30

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

代码语言:javascript
复制
NSMutableArray *resultArray = [[NSMutableArray] alloc] init];
for (NSDictionary *tweet in dict)
{
     [resultArray addObject:tweet];
}

然后在cellForRowAtIndexPath中,根据表的行来检索对象,如下所示

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10918160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档