View更改后,TableView不显示数据

时间:2012-01-12 15:02:54

标签: ios nsuserdefaults tableview

我有问题。目前我正在抛光我的应用程序并调试它等等。发生了一件奇怪的事。以下是它的工作原理:用户输入数据并进入TableView并显示数据。简单。但是一旦他切换到另一个视图然后回到TableView,数据就会消失!奇怪的是它以前没有发生过。 (我重写了一些代码,也许我在不知不觉中改变了一些东西)。我有一个可能是什么,但它会很奇怪,因为它之前有用。我认为问题出在buttonTag行:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

  // cell.image = selectedImage;
cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:14.0];

if (buttonTag == 9001) {    
cell.textLabel.text = [NSString stringWithFormat:@"%@  %@ %@,  %@.", [self.userData objectAtIndex:0], [self.userData objectAtIndex:1], [self.userData objectAtIndex:2], [self.userData objectAtIndex:3]];
    }
if (buttonTag == 9002) {    
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@, %@.", [self.userData objectAtIndex:0], [self.userData objectAtIndex:1], [self.userData objectAtIndex:2], [self.userData objectAtIndex:3]];
}
if (buttonTag == 9003) {    
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@ %@,%@.", [self.userData objectAtIndex:0], [self.userData objectAtIndex:1], [self.userData objectAtIndex:2], [self.userData objectAtIndex:3]];
}


return cell;

}

因为他只是在按下特定按钮时显示数据。如果您在没有按下其中一个按钮的情况下更改为TableView(例如,从Tabbar中)它不会显示任何内容。我现在的问题是:如何判断TableView是否按下了最后一个按钮的数据? (我在考虑NSUserDefaults?)

这是另一个ViewController,你可以从那里看到TableView:

  - (IBAction)savePressed:(id)sender

{
if (buttonTag == 9001) {
     button.tag = 9001;
NSUserDefaults *savetext = [NSUserDefaults standardUserDefaults];
[savetext setObject:Antwort.text forKey:@"hallo"];
[savetext setObject:Antwort2.text forKey:@"hallo2"];
[savetext setObject:Antwort3.text forKey:@"hallo3"];
UIButton *buttonPressed = (UIButton *)sender;
TV *second =[[TV alloc] initWithNibName:nil bundle:nil];
second.buttonTag  = buttonPressed.tag; 
    [self.navigationController pushViewController:second animated:YES];

}
if (buttonTag == 9002) {
     button.tag = 9002;
    NSUserDefaults *savetext = [NSUserDefaults standardUserDefaults];
    [savetext setObject:Antwort.text forKey:@"hallo"];
    [savetext setObject:Antwort2.text forKey:@"hallo2"];
    [savetext setObject:Antwort3.text forKey:@"hallo3"];
    UIButton *buttonPressed = (UIButton *)sender;
    TV *second =[[TV alloc] initWithNibName:nil bundle:nil];
    second.buttonTag  = buttonPressed.tag; 
  [self.navigationController pushViewController:second animated:YES];

}
if (buttonTag == 9003) {
    button.tag = 9003;
    NSUserDefaults *savetext = [NSUserDefaults standardUserDefaults];
    [savetext setObject:Antwort.text forKey:@"hallo"];
    [savetext setObject:Antwort2.text forKey:@"hallo2"];
    [savetext setObject:Antwort3.text forKey:@"hallo3"];
    UIButton *buttonPressed = (UIButton *)sender;
    TV *second =[[TV alloc] initWithNibName:nil bundle:nil];
    second.buttonTag  = buttonPressed.tag; 
[self.navigationController pushViewController:second animated:YES];

}

}

1 个答案:

答案 0 :(得分:1)

如果读取数据&保存在NSUSerDefaults中,尝试

[savetext synchronize] 

在推向新观点之前。