UITableViewCell使用objectForKey返回null

时间:2013-10-08 06:46:20

标签: iphone uitableview nsmutablearray nsuserdefaults

我在VC中有textview。使用nsuserdefaults保存此textview并稍后获取。在VC1中我得到了保存的textview并在UITableView中显示。但是当我启动应用程序时,它会自动在索引0中显示“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;

    }

1 个答案:

答案 0 :(得分:0)

我也有这个问题。 NSUserDefaults无法处理可变对象。 尝试将您的字符串存储到CoreData中。它简单而有力。我做到了,它完美无缺。 如果您没有使用CoreData的技巧,请参阅教程: http://www.youtube.com/watch?v=StMBHzA7h18

Step1创建新文件 - 数据模型 Step2创建实体(名称)那里添加一个属性(name2) 这么简单!