【问题标题】:save and load data in UItableview在 UItableview 中保存和加载数据
【发布时间】:2017-03-11 20:01:58
【问题描述】:

我在 UITableView 中保存和加载数据时遇到了一些问题。按button2后,我找不到我保存的数据(可能我没有保存)。

我的.h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray * arrayIdea;
    NSMutableArray * arrayEdit;

    IBOutlet UITableViewCell * cell;

}
@property(nonatomic,strong)IBOutlet UITextField * txtField;
@property(nonatomic,strong)IBOutlet UITableView * tabel1;
-(IBAction)button2:(id)sender;
-(IBAction)button1:(id)sender;
@end

我的 .m 文件:

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

    arrayIdea = [[NSMutableArray alloc] init];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (arrayIdea.count > 0) {
        return arrayIdea.count;

    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];

    }
    cell.textLabel.text =[NSString stringWithFormat:@"%@", arrayIdea[indexPath.row]];

    return cell;
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete) {
        [arrayIdea removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation: UITableViewRowAnimationFade];
    }
}
-(IBAction)button1:(id)sender;{

    [arrayIdea addObject:self.txtField.text];
    [self.tabel1 reloadData];
     self.txtField.text = @"";
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:arrayIdea forKey:@"savedstring"];
    [defaults synchronize];
}


-(IBAction)button2:(id)sender;{
    NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
    cell.textLabel.text =[defaults objectForKey:@"savedstring"];
}

@end

【问题讨论】:

  • 我试过你的代码,似乎代码在cell.textLabel.text =[defaults objectForKey:@"savedstring"];这一行崩溃了,因为你正在保存一个数组并试图将数组分配到一个文本字段中。而是尝试检索回数组并分配到文本字段中。希望这些有所帮助。

标签: ios objective-c uitableview nsarray nsuserdefaults


【解决方案1】:

您需要对数组进行编码/解码才能在 NSUserDefaults 中保存/检索它。

保存

NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject: arrayIdea];
[[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"savedstring"];
[[NSUserDefaults standardUserDefaults] synchronize];

阅读

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedstring"];
NSArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

【讨论】:

  • 感谢您回答我的问题!我试过了,但它说“使用未声明的标识符'currentDefault”,所以我想知道在哪里可以定义它。
  • 现在检查,currentDefault 只不过是 NSUserDefault 对象。我以为你会自己想办法。
  • 好的!谢谢!
  • @TonyChen 如果您从我的回答中获得帮助,请投票并接受作为回答。
【解决方案2】:

您可以像这样保存和检索..

保存

[[NSUserDefaults standardUserDefaults]setObject:arrayIdea forKey:@"savedstring"];

并检索

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

【讨论】:

    【解决方案3】:
    In cellForRowAtIndexpath, If you write code like below.
    
    cell.textlabel.text = [[defaults objectForKey:@"saved string"] objectAtIndex:indexPath.row]; 
    
    instead of passing array.
    In numberOfRowsInSection you can pass
    
    [[defalts objectForKey:@"saved string"] count];
    
    You already called table reload method, so there is no need of button 2.
    Please try.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2020-07-31
      • 2014-05-11
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多