如何将NSMutableArray存储为NSUserDefaults并在TableView中显示结果



我想在应用程序中保存来自服务器的通知,并制作一个用户界面,让用户能够选择读取哪个通知(消息)。在一个调度的方法中,我的客户端控制服务器内部的更改,并且通信是JSON格式的。我已经解析了它,并且可以在NSLog(@"....",..)中看到结果。我还控制来自服务器的消息的状态,如果状态等于1,我将保存消息并向TableView添加一个节点。。现在,有人能帮助我如何将NSMutableArray中的数据传输到NSUserDefaults和TableView吗?如果你愿意,我也可以共享代码或JSON表示。。如果你能用一些代码来解释会更好。。感谢

我决定分享我的一些代码,

由于我也在代码下面写了,我想在UITableView 中显示NSMutableArray

    `-(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

`

将其保存在NSUserDefaults:中

[[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,所以我的回答可能有点草率。

在头文件中,不要忘记添加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

相关内容

最新更新