2012-05-12 29 views
0

我正在使用我的Twitter應用程序。我在TableView中獲取搜索結果。當我刷新搜索結果時,表格會填充新的傳入推文,而較早的推文會消失。任何人都可以建議我添加新推文以及更早的推文嗎?除了它已包含的以前的數據之外,如何向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]; 

}

+0

你可以添加一些代碼,你如何保存你的新數據? – rishi

+0

我認爲你只有50條推文。我對麼? –

+0

不,我收到15條(默認)推文。 –

回答

1

您還沒有給出實現代碼如下代表code.You需要ADDOBJECT給您檢索網頁service.Before擊中Web服務的新數據的NSMutableArray每次得到的tableview的數組(可變)您正在使用並在解析後從Web服務添加對象。將此數組分配給tableview數組,然後重新加載表。

+0

同意。我會這麼做的方式是使用[table reloadData],並且委託方法將從您的數組中更新您的表。 – geminiCoder

相關問題