2012-01-11 98 views
0

我想在應用程序中保存來自服務器的通知,並且創建用戶界面以使用戶能夠選擇通知(消息)來讀取。在預定的方法中,我的客戶端控制服務器內的更改,並且通信採用JSON格式。我已經解析過它,並且也可以在NSLog(@"....",..)中看到結果。我也控制從服務器的消息狀態,如果狀態等於1,我將保存消息並添加一個節點到TableView ..現在,誰能幫我關於如何在NSUserDefaults和TableView中傳輸NSMutableArray中的數據?我可以分享代碼或JSON表示過,如果你想.. 這將是更好,如果你可以用一些代碼解釋..謝謝如何將NSMutableArray存儲到NSUserDefaults並在TableView中顯示結果?

我決定分享我的一些代碼,

,因爲我已經下writen代碼也一樣,我想在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

NSUserDefaults的中顯示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; 
} 

希望它可以幫助

更新

沒有此刻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的響應後,我會創造新的觀點,所以應該在哪裏我把這個代碼(或者我需要使用之前在storybord創建的tableView方法?)? – 2012-01-11 16:16:14

+0

@ilhançetincellForRowAtIndexPath方法應該已經存在於具有UITableView的控制器中。 NSUserDefaults可以從任何地方訪問。 – Novarg 2012-01-11 16:19:30

+0

我定義了UITableView * tableView;它的屬性在我的.h文件中,並將它進行了系統化,後來使用了你的方法,但沒有發生任何事情。 我將多解釋一下,我定義了一個名爲Accept的動作,當我按下它的按鈕(調用它)時,我用NSUserDefaults將我的登錄信息保存在我的應用程序中,同時將我的值發送到Server,並接收JSON bac在那個行動..我還沒有創建一個TableView,現在我有兩個問題.. 1)我必須創建一個TableView? 2)我會在哪裏使用這段代碼來運行它? 如果問題看起來很愚蠢,請原諒我 – 2012-01-11 16:31:10

相關問題