2017-03-11 106 views
0

我在保存和加載UITableView中的數據時遇到了一些問題。按下按鈕2後,我找不到保存的數據(也許我沒有保存)。在UItableview中保存和加載數據

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

我想你的代碼,它似乎在代碼此行'cell.textLabel.text =崩潰[默認objectForKey:@ 「savedstring」];'因爲您正在保存一個數組並嘗試將該數組分配到一個文本字段中。相反,嘗試恢復到陣列並分配到文本字段。希望這些幫助。 –

回答

0

你需要編碼/解碼你的陣列能夠保存/ 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]; 
+0

謝謝你回答我的問題!我試過了,但它說「使用未聲明的標識符currentDefault」,所以我想知道我可以在哪裏定義它。 – TonyChen

+0

現在檢查,currentDefault只是NSUserDefault對象。我以爲你會自己弄明白。 – Pawan

+0

好吧!謝謝! – 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

您可以保存和檢索這樣的..

保存

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

和檢索

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