2012-01-11 20 views
0

私は自分のアプリケーションにサーバーから来た通知を保存し、ユーザーがどの通知(メッセージ)を読むべきかを選択できるようにユーザーインターフェイスを作成したいと思います。スケジュールされたメソッドでは、クライアントはサーバー内の変更を制御し、通信はJSON形式です。私はそれを解析して、結果をNSLog(@"....",..)でも見ることができます。私はまた、ステータスが1に等しい場合、サーバーからのメッセージの状態を制御します。メッセージを保存し、TableViewにノードを追加します。NSMutableArrayのデータをNSUserDefaultsとTableViewの両方に送信する方法について教えてください。あなたがしたい場合、私は下に書かれているように私は、私は私のコードの一部を共有することを決めたおかげNSMutableArrayをNSUserDefaultsに保存し、TableViewで結果を表示するにはどうすればいいですか?

..あなたは、いくつかのコードで説明できる場合、それは良くなる...あまりにも

を をコードまたは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からそれを得る:

のUITableViewCellに配列から値を設定
[[NSUserDefaults standardUserDefaults]objectForKey:@"theArray"]; 

- (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; 
} 

はそれが

更新

が持っていないお役に立てば幸いです現時点ではMacが近くにありますので、私の答えは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があるはずです。 UITableViewについての詳細情報については

そのリンクをチェックしてください:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

+0

私がGETの応答後に新しいビューを作成しますので、ここで、iは、このコードを配置しなければならない(または私はこれを使用する前にstorybordでのtableViewを作成する必要があります方法?)? –

+0

@ilhançetincellForRowAtIndexPathメソッドは、すでにコントローラにUITableViewとともに存在しているはずです。 NSUserDefaultsはどこからでもアクセスできます。 – Novarg

+0

私はUITableView * tableViewを定義し、そのプロパティを.hファイルに入れてsysnthesizeし、後であなたのメソッドを使用しましたが、何も起こりませんでした。 もう少し説明します。私はAcceptと呼ばれるアクションを定義しています。そのボタンを押すと、NSUserDefaultsを使用してアプリケーションにログイン情報を保存し、同時にサーバーに値を送信し、JSON bacを受信しますそのアクションで..私はまだTableViewを作成していない、今私は2つの質問があります.. 1)私はTableViewを作成する必要がありますか? 2)このコードを実行して実行させる場所はどこですか? 質問が愚かであれば私を許してください –

関連する問題