2013-10-08 45 views
-1

我有VC中的textview。使用nsuserdefaults保存這個textview並在以後得到它。在VC1即時通訊保存textview並顯示在UITableView。但是,當我啓動應用它automcatically顯示 「空」 文本索引0UITableViewCell返回null使用objectForKey

VC:

-(void)save:(id)sender{ 

    NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults]; 
    [userData1 setObject:textView.text forKey:@"savetext"]; 
    [userData1 synchronize]; 
} 

VC1:

-(void) viewWillAppear:(BOOL)animated{ 

    [super viewWillAppear:animated]; 


textArray=[[NSMutableArray alloc]init]; 

    txt=[[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)]; 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    // getting an NSString 

    NSString *savedValue = [prefs stringForKey:@"savetext"]; 

    txt.text = [NSString stringWithFormat:@"%@", savedValue]; 

    MyAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 


    // [MyAppDelegate.textArray addObject:txt.text]; 

     if(![MyAppDelegate.textArray containsObject:txt.text]){ 
     [MyAppDelegate.textArray addObject:txt.text]; 


      NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults]; 
      [userData1 setObject:MyAppDelegate.textArray forKey:@"save"]; 
      [userData1 synchronize]; 

    } 



    [self.view addSubview:txt]; 

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)           style:UITableViewStylePlain]; 
    NSLog(@"Scrolling"); 

    tableView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | 
    UIViewAutoresizingFlexibleWidth | 
    UIViewAutoresizingFlexibleRightMargin; 

    // tableView.contentInset = UIEdgeInsetsMake(0, 0,300, 0); //values passed are - top, left, bottom, right 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    [tableView reloadData]; 

    tableView.contentInset = UIEdgeInsetsMake(0, 0,300, 0); 


    //self.view = tableView; 
    [self.view addSubview:tableView]; 


     } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 


     NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]]; 

     NSLog(@"count is %@",myMutableArrayAgain); 


     return [myMutableArrayAgain count]; 


    } 




    - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


     NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]]; 


     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier ]; 

     NSLog(@"cell is %@",cell); 

     if (cell == nil) { 

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      NSLog(@"cell is %@",cell); 


     } 

     // Configure the cell... 


     cell.textLabel.text = [myMutableArrayAgain objectAtIndex:indexPath.row]; 
     [cell.textLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     return cell; 

    } 
+0

顯示您在NSUserdefault中保存文本的代碼。 –

+0

查看編輯後的代碼.. – user2807197

+0

可能是表重新加載導致錯誤? – user2807197

回答

0

我也有這個問題。 NSUserDefaults不能使用可變對象進行處理。 嘗試將您的字符串存儲到CoreData中。它簡單而強大。我做到了,它完美無缺。 如果你沒有技能CoreData這裏是教程: http://www.youtube.com/watch?v=StMBHzA7h18

第一步創建新的文件 - 數據模型 第二步:創建實體(名)有添加一個屬性附加傷害(名2) 這麼簡單!

相關問題