我有一个 TWRequest,它创建了一个名为 dict 的字典输出。我想将此结果输出到 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;
}