2012-01-11 69 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

相关问题