自动观察 UITableView 中的文件系统变化
我希望有人可以建议一种方法,让我可以使用 UITableView 自动“观察”文件系统中的更改。
我有一个 UITableView,其中填充了目录中文件的内容。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];
然后,我在 cellForRowAtIndexPath 中使用该数组来显示项目。现在,如果我添加对删除项目的支持,则需要添加一个步骤:我必须删除物理文件并更新我的阵列。
一定有更好的方法,但尽管进行了大量搜索,我还是找不到。
谢谢!
I'm hoping someone could suggest a way that I could automatically "observe" changes in a filesystem with a UITableView.
I have a UITableView populated with the contents of files in my directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];
I then use this array in cellForRowAtIndexPath to display items. Now, if I add support for deleting items, there's an added step necessary: I have to both delete the physical file and update my array.
There's got to be a better way but I can't find it despite much searching.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常这可以通过
NSWorkspace
来实现...iPhone不支持它。但是,由于除了您之外没有人应该在文档目录中写入内容,因此您可以轻松地实现一个包含此“添加的额外步骤”的解决方案。我不认为这太不雅了。Usually this can be achieved by
NSWorkspace
... which is not supported on the iPhone. But since nobody except you should write in your documents directory you can easily implement a solution which includes this "added extra step". I do not think that this is too inelegant.您可以将 NSFileManager 包装在您自己的类中,该类将通过 KVO、NSNotification 或临时委托通知您的代码。这样的类可以很容易地在不同的项目中重用;这是此类的标头片段:
You could wrap NSFileManager in a class of your own, and this class would notify your code via KVO, NSNotification or an ad-hoc delegate. Such a class could be easily reused through different projects; here goes a fragment of the header of such a class: