【问题标题】:NSFilemanager doesn't save file when textFieldDidEndEditing is called a second time第二次调用 textFieldDidEndEditing 时 NSFilemanager 不保存文件
【发布时间】:2011-11-21 03:05:47
【问题描述】:

我无法更新我的tableView

rootviewcontroller管理的popover的tableView中,显示了我的文档目录中的一些项目。在detailViewcontroller 中,我使用NSFilemanager 更改了这些文件的名称。无论我做什么,tableView 都不会显示新的。如果我关闭应用程序并再次打开它,我会看到它们。目前,我尝试使用通知,但它不起作用……

编辑

我记录了我的文档目录,肯定不是tableView的问题,而且效果很好,但是第二次输入一些文本,什么都没有发生……

DetailViewController

- (void)textFieldDidEndEditing:(UITextField *)tf
{
    textLabel.text = textField.text;
    NSString* newName = textLabel.text;
    newName = [newName stringByAppendingPathExtension:@"txt"];
    NSString* newPath = [[currentFilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName]; 
    NSLog(@"%@",newPath);
    [[NSFileManager defaultManager] moveItemAtPath:currentFilePath toPath:newPath error:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];

}

RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
    self.clearsSelectionOnViewWillAppear = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSaved:) name:@"DataSaved" object:nil];
}

- (void)dataSaved:(NSNotification *)notification
{
    [self loadDirectoryContents];
    [self.tableView reloadData];
}

【问题讨论】:

  • 哦 loadDirectoryContents 效果很好顺便说一句

标签: ios ipad file uitableview reload


【解决方案1】:
[self.tableView initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];

这条线看起来有点可疑。你是如何构建你的 tableview 的?

【讨论】:

  • 我正在以编程方式进行。所以也使用tableView:cellForRowAtIndexPath。表视图运行良好。当我打开和关闭应用程序时,它也会更新。但我想立即更新它;)
  • 你是否也在使用 UITableViewController,或者你创建了一个 nib 文件?除非您已经在其他地方初始化了 tableView,否则我提到的行没有意义。您的代码中的 [UITableView alloc] 在哪里?
  • tableView = [[UITableView alloc] initWithFrame:yourFrame style:UITableViewStyleGrouped]; //如果您仅以编程方式创建表,这就是我希望看到的
  • 我刚刚使用拆分视图模板创建了一个新项目。没有任何 UITableView,所以你必须添加这个。你把它添加到 DefaultView.xib 了吗?
  • 当我打开模板并构建运行时,在 potrait 模式下,工具栏(左上角)有一个按钮,上面写着“事件”。如果单击它,则会打开一个标题为“根视图控制器”的弹出窗口。在弹出窗口中,有一个空表视图。在 RootViewController 类中还有一堆 tableView 方法。哦,那 initWithFrame 的东西是我添加的,因为我希望 tableView 具有分组样式。
【解决方案2】:

当我输入一些东西时它会起作用。但是,如果我第二次输入某些内容而没有在 tableView 中选择任何内容,则 fileManager 不会将该项目移动到路径中。有什么建议?好像不是tableView相关的问题。

我还认为我需要设置:

currentFilePath = newPath;

但这给了我一个 EXC_BAD_ACCESS。

【讨论】:

  • 我需要保留:[currentFilePath retain];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 2012-09-24
  • 2011-10-11
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多