2012-05-12 8 views
0

나는 내 트위터 앱을 가지고 일하고있다. TableView에서 검색 결과를 가져오고 있습니다. 검색 결과를 새로 고칠 때 테이블에 새로운 들어오는 짹짹이 표시되고 이전 테이블은 사라집니다. 어느 누구도 저의 트윗과 함께 새로운 트윗을 추가 할 수있는 방법을 제안 할 수 있습니까? 당신은있는 tableview 대표 code.You 부여하지 않은이미 포함되어있는 이전 데이터 이외에 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

이있는 tableview의 배열 (가변) 얻을 당신이 웹 서비스를 타격 웹 service.Before에서 새 데이터를 검색 NSMutableArray를 매번에 addobject 필요 당신이 사용하고 parsing.Assign이 배열을 tableview 배열 및 다음 테이블을 다시로드 후 웹 서비스에서 개체를 추가합니다.

+0

동의 함. 내가 그것을 할 방법은 [테이블 reloadData] 및 델리게이트 메서드는 배열에서 테이블을 업데이 트됩니다 사용됩니다. – geminiCoder

관련 문제