我正在尝试使用带有TWRequest结果的自定义单元格传播表格视图而没有运气。我试图将推文存储在字典对象中,然后调用ObjectAtKey请求并放入字符串。这是我的代码 - 任何帮助将不胜感激。谢谢 -
-(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;
}
答案 0 :(得分:1)
您需要从Twitter响应中创建推文的NSMutableArray。 每条推文都是一个单独的对象。
NSMutableArray *resultArray = [[NSMutableArray] alloc] init];
for (NSDictionary *tweet in dict)
{
[resultArray addObject:tweet];
}
然后在cellForRowAtIndexPath
中根据表格的行检索对象,如
TweetResult *tweetResult [resultArray objectAtIndex:[indexPath row]];