2013-10-08 46 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) 这么简单!

相关问题