2017-03-11 4 views
0

Ich habe ein Problem mit dem Speichern und Laden von Daten in UITableView. Nachdem ich Taste2 gedrückt habe, kann ich meine gespeicherten Daten nicht finden (vielleicht habe ich sie nicht gespeichert).Speichern und Laden von Daten in UItableview

My.h Datei:

#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 

Meine .m-Datei:

#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 
+0

habe ich versucht, den Code und es scheint der Code auf dieser Linie abstürzt 'cell.textLabel.text = [defaults objectForKey: @ "savedstring"];', da Sie ein Array speichern und versuchen, das Array einem Textfeld zuzuordnen. Versuchen Sie stattdessen, zurück zum Array zu gelangen und dem Textfeld zuzuordnen. Hoffe, das hilft. –

Antwort

0

Sie benötigen Array zu kodieren/dekodieren zu können, um sie speichern/abrufen es in NSUserDefaults.

Speichern

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

lesen

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

Vielen Dank für die Beantwortung meiner Frage! Ich habe versucht, aber es heißt "Verwenden Sie nicht deklarierte Bezeichner 'currentDefault", also frage ich mich, wo ich es definieren kann. – TonyChen

+0

Überprüfen Sie jetzt, currentDefault ist nichts anderes als NSUserDefault Objekt. Ich dachte, du wirst es selbst herausfinden. – Pawan

+0

Okay! Vielen Dank! – TonyChen

0
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. 
0

Sie können wie diese speichern und abrufen ..

Speichern

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

und abrufen

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