我有个问题。目前,我正在完善我的应用程序并对其进行调试等。发生了一些奇怪的事情。它是这样工作的:用户输入数据并进入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(例如从选项卡栏中),则不会显示任何内容。现在的问题是:如何告诉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];

}


}

最佳答案

如果已读取数据并将其保存在NSUSerDefaults中,请尝试

[savetext synchronize]


然后再推向新的观点。

关于ios - 更改 View 后,TableView不显示数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8837206/

10-09 08:06
查看更多