2013-10-08 3 views
-1

VC에서 textview가 있습니다. nsuserdefaults를 사용하여이 텍스트 뷰를 저장하고 나중에 가져옵니다. VC1 인스턴트 메신저 저장 textview 및 UITableView에서 표시 점점. 인덱스에 내가 응용 프로그램을 실행할 때이 automcatically 표시 "널 (null)"텍스트 0UITableViewCell은 objectForKey를 사용하여 null을 반환합니다.

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에 저장하십시오. 간단하고 강력합니까? 나는 그것을 만들고 완벽하게 작동합니다. http://www.youtube.com/watch?v=StMBHzA7h18

1 단계는 새 파일 생성 - 데이터 모델을 2 단계가 하나 atribute (NAME2) 를 추가 엔티티 (이름)을 만들 그래서 간단한 : 당신이 CoreData하지 능력이있는 경우 여기 튜토리얼입니다!

관련 문제