0

我正在使用我的 Twitter 应用程序。我在 TableView 中获取搜索结果。当我刷新搜索结果时,表格会填充新传入的推文,而较早的推文会消失。任何人都可以建议我一种在早期推文中添加新推文的方法吗?

//my array
-(NSMutableArray *)retrievedTweets
{
if (retrievedTweets == nil)
{
    retrievedTweets = [NSMutableArray arrayWithCapacity:50];
}
return retrievedTweets;
}


-(BOOL)checkCanTweet
{
if ([TWTweetComposeViewController canSendTweet])
{
    self.goButton.enabled = YES;
    self.goButton.alpha = 1.0;
    return YES;
}
else
{
    self.goButton.enabled = NO;
    self.goButton.alpha = 0.6;
    return NO;
}
}

//search function

-(void)searchTweet{

if (retrievedTweets == nil) {
    retrievedTweets=[[NSMutableArray alloc] init];
}
//retrievedTweets = nil;

if ([self checkCanTweet])
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = 
    [accountStore
     accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter ];
    [accountStore requestAccessToAccountsWithType:accountType
                            withCompletionHandler:^(BOOL granted, NSError *error)
     {
         if (granted)
         {
             NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

             if ([accountsArray count] >0)
             {
                 [self.retrievedTweets removeAllObjects];

                 NSString *str1 = [[NSString alloc]initWithFormat:@"http://search.twitter.com/search.json?q="];
                 //NSString *str2 = [[NSString alloc]initWithFormat:@"http://search.twitter.com/search.json?q=%23"];


                 NSString *textString = searchBarText.text;
                 NSString *urlString = [[NSString alloc]init];

                 if(textString==nil)
                 {
                     self.goButton.enabled = NO;
                 }
                 else {

                     self.goButton.enabled = YES;

                     unichar  c = [textString characterAtIndex:0];

                     if(c == '#'){

                         NSString *newStr = [textString substringWithRange:NSMakeRange(1, [textString length]-1)];
                         urlString=[str1 stringByAppendingFormat:newStr];
                     }
                     else {

                         urlString = [str1 stringByAppendingFormat:searchBarText.text];
                     }
                 }



                 ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
                 TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:urlString] parameters:nil
                                                           requestMethod:TWRequestMethodGET];

                 [postRequest setAccount:twitterAccount];

                 [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      if ([urlResponse statusCode] == 200)
                      {
                          NSError *jsonParsingError;
                          NSDictionary *homeTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];

                          NSDictionary *results=[homeTimeline objectForKey:@"results"];
                          Search *current;


                          for (NSDictionary *dict in results)
                          {


                              current = [[Search alloc] initWithDictionary:dict];
                              [self.retrievedTweets addObject:current];
                          }

                          dispatch_async(dispatch_get_main_queue(), ^{


                              [self.tableViewPost reloadData];

                          });
                      }
                      else
                      {
                          NSLog(@"%@", [NSString stringWithFormat:@"HTTP response status: %i\n", [urlResponse statusCode]]);
                      }
                      //   [self.tableViewPost reloadData];
                  }];
             }
         }
         else
         {
             NSLog(@"Error, Twitter account access not granted.");
         }
     }];
}

[searchBarText resignFirstResponder];

}

4

1 回答 1

1

您尚未提供 tableview 委托代码。每次从 Web 服务检索新数据时,您都需要将对象添加到 NSMutableArray。在访问 Web 服务之前,获取您正在使用的 tableview 的数组(可变)并从 Web 向其​​中添加对象解析后服务。将此数组分配给tableview数组,然后重新加载表。

于 2012-05-12T10:59:58.527 回答