【问题标题】:UITableViewCell returns null using objectForKeyUITableViewCell 使用 objectForKey 返回 null
【发布时间】:2013-10-08 06:46:19
【问题描述】:

我在 VC 中有 textview。使用 nsuserdefaults 保存此文本视图并稍后获取。在 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;

    }

【问题讨论】:

  • 显示你在 NSUserdefault 中保存文本的代码。
  • 可能是表重载导致错误?
  • 这可能是因为您保存了空文本检查我的代码我在保存文本时给出了条件。删除应用程序并尝试再次运行并检查。
  • 我正在重新初始化这个 NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];但是成员变量不适用于此
  • 第一件事你永远不应该在 viewWillAppear 中初始化你的视图对象,而是在 viewDidLoad 中进行。

标签: iphone uitableview nsmutablearray nsuserdefaults


【解决方案1】:

我也有这个问题。 NSUserDefaults 不能处理可变对象。 尝试将您的字符串存储到 CoreData。是不是简单又强大。我做到了,而且效果很好。 如果您没有使用 CoreData 的技能,这里是教程: http://www.youtube.com/watch?v=StMBHzA7h18

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

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多