2012-01-11 4 views
0

내 응용 프로그램에서 서버에서 가져온 알림을 저장하고 사용자가 어떤 알림 (메시지)을 읽을 지 선택할 수있는 기능을 제공하기 위해 사용자 인터페이스를 만들고 싶습니다. 스케줄 된 메소드에서 클라이언트는 서버 내부의 변경 사항을 제어하며 통신은 JSON 형식입니다. 나는 그것을 파싱했고 NSLog(@"....",..)에서도 결과를 볼 수있다. 나는 또한 상태가 1 인 경우 서버에서 메시지의 상태를 제어합니다. 메시지를 저장하고 TableView에 노드를 추가합니다. 이제는 아무도 데이터를 NSMutableArray에 전송하는 방법에 대해 NSUserDefaults과 TableView에 모두 도움이 될 수 있습니까? 당신이 원한다면 나는 내 코드의 일부를 공유하기로 결정NSMutableArray를 NSUserDefaults에 저장하고 TableView에 결과를 표시하는 방법은 무엇입니까?

나는

전에서 writen 것처럼, 감사합니다 ... 당신은 몇 가지 코드를 설명 할 수 있다면 더 좋을 것이다 .. 너무 코드 또는 JSON 표현을 공유 할 수 있습니다 코드도 내가 NSUserDefaults에 저장 UITableView

`-(IBAction)Accept:(id)sender 
    { userName=[[NSString alloc] initWithString:userNameField.text ]; 
     [userNameField setText:userName]; 
     NSUserDefaults *userNameDef= [NSUserDefaults standardUserDefaults]; 
     [userNameDef setObject:userName forKey:@"userNameKey"]; 
     password =[[NSString alloc] initWithString:passwordField.text]; 
     [passwordField setText:password]; 
     NSUserDefaults *passDef=[NSUserDefaults standardUserDefaults]; 
     [passDef setObject:password forKey:@"passwordKey"]; 
     serverIP=[[NSString alloc] initWithString: serverField.text]; 
     [serverField setText:serverIP]; 
     NSUserDefaults *serverDef=[NSUserDefaults standardUserDefaults]; 
     [serverDef setObject:serverIP forKey:@"serverIPKey"]; 
     [userNameDef synchronize]; 
     [serverDef synchronize]; 
     [passDef synchronize]; 


    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"BNTPRO " 
                 message:@"Your User Informations are going to be sent to server. Do you accept?" 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:@"Cancel", nil]; 
    [message show]; 
} 



    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

     if([title isEqualToString:@"OK"]) 
     { 
      if([userNameField.text isEqualToString:@""]|| [passwordField.text isEqualToString:@""] || [serverField.text length]<10) 
      { 
       UIAlertView *message1 = [[UIAlertView alloc] initWithTitle:@"BNTPRO " 
                    message:@"Your User Informations are not defined properly!" 
                    delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles: nil]; 

       [message1 show]; 


     [userNameField resignFirstResponder]; 
      [passwordField resignFirstResponder]; 
      return; 
     } 
     //## GET code to here** 
     NSString *str1=[@"?username=" stringByAppendingString:userNameField.text]; 
     NSString *str2=[@"&password=" stringByAppendingString:passwordField.text]; 
     NSString *str3=[str1 stringByAppendingString:str2]; 
     NSString *str4 =[@"http://" stringByAppendingString:serverField.text]; 


     NSURL *url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]]; 
     NSLog(@"%@\n",url); 
     //get the url to jsondata 
     NSData *jSonData=[NSData dataWithContentsOfURL:url]; 

     if (jSonData!=nil) { 
      NSError *error=nil; 
      id result=[NSJSONSerialization JSONObjectWithData:jSonData options: 
         NSJSONReadingMutableContainers error:&error]; 
      if (error==nil) { 
       NSDictionary *mess=[result objectForKey:@"message"]; 
       NSDictionary *messContent=[mess valueForKeyPath:@"message"]; 
       NSDictionary *messDate=[mess valueForKeyPath:@"date"]; 
       NSDictionary *messID=[mess valueForKeyPath:@"ID"]; 
       NSDictionary *messStatus=[mess valueForKey:@"status"]; 

       NSLog(@"%@ *** Message %@ \n Message Content: %@ \n Mesage ID: %@ \n Message Date: %@\n \nilhancetin MessageSatus: %@", result, mess, messContent, messID,messDate,messStatus); 
       NSString*key1=[ result objectForKey:@"key" ]; 

       NSString *s1=[@"http://" stringByAppendingString:serverField.text]; 
       NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"]; 
       NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]]; 
       NSLog(@"\n%@\n",url2); 
       NSData *data2=[NSData dataWithContentsOfURL:url2]; 
       id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil]; 

       NSMutableArray *mesID = [NSMutableArray array];//saving meesages to NSMutableArray 
       NSMutableArray *status = [NSMutableArray array];  


       // i logged here and it saves the data, now i want to display my data in table view 

`

답변

2

NSMutableArray를 표시하려면 :

[[NSUserDefaults standardUserDefaults]setObject:yourArray forKey:@"theArray"]; 

은 NSUserDefaults에서 그것을 얻을 :

[[NSUserDefaults standardUserDefaults]objectForKey:@"theArray"]; 

가있는 UITableViewCell에 배열에서 값을 설정 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"TheCell"; 
    UITableViewCell *_cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (_cell == nil) { 
     _cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    _cell.textLabel.text = [yourArray objectAtIndex:indexPath.row]; 
    return _cell; 
} 

그것이

갱신

이 없어 도움이되기를 바랍니다 잠시 가까이에 맥이 있으니 제 답변은 b 조금 어색해. 헤더 파일에서

는 UITableViewDelegate 및 UITableViewDataSource를 추가하는 것을 잊지 마세요, 그래서 다소처럼 보일 것이다 :

@interface yourController : UIViewController <UITableViewDelegate, UITableViewDataSource, ... some others if you need it ...> { 

다음 구현 파일 (하는 .m)에 그냥 입력을 시작할 수 있습니다

-tableview 

그리고 자동 완성을 사용하여 필요한 방법을 얻으십시오. 다음과 같은 3 가지가 가장 필요할 것입니다.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 

앱의 필요에 따라 더 많이 필요할 수도 있지만 3 가지가 있어야합니다. http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

+0

내가 GET의 반응 후에 새로운 뷰를 만들 것이다, 그래서이 코드를 삽입 (또는 할까 어디에서 이것을 사용하기 전에 storybord에있는 tableView를 만들 필요가 : 링크를 확인하시기 바랍니다 jQuery과에 대한 자세한 정보를 들어

방법?)? –

+0

@ilhançetin cellForRowAtIndexPath 메서드가 이미 컨트롤러에 UITableView와 함께 있어야합니다. NSUserDefaults는 어디서든 액세스 할 수 있습니다. – Novarg

+0

UITableView * tableView를 정의하고 내 .h 파일의 해당 속성을 sysnthesized하여 나중에 메서드를 사용했지만 아무 일도 없었습니다. 좀 더 설명해 드리겠습니다. 수락이라는 동작을 정의하고 버튼을 누르면 (호출이라고 함) NSUserDefaults로 내 앱에 로그인 정보를 저장하고 동시에 내 값을 서버에 보내고 JSON 백신을 수신합니다 그 행동에 .. 아직 TableView를 만들지 않은데, 이제는 두 가지 질문이 있습니다. 1) TableView를 만들어야하나요? 2)이 코드를 실행하기 위해 어디에서 코드를 작성합니까? 질문이 어리석은 경우에 나를 용서하십시오 –

관련 문제