【问题标题】:Populating TableView with Plist Documents Directory in iOS在 iOS 中使用 Plist 文档目录填充 TableView
【发布时间】:2014-04-19 03:47:49
【问题描述】:
  • 我是Objective-C 的新手,非常感谢任何帮助。
  • 我有一个TableView,它成功地从我的 Xcode 项目中的Data.plist 文件中拉回列表,但我需要它从 文档目录 中拉取。
  • 我看过很多关于此的帖子,但似乎无法让它为我工作。
  • 下面是我的 .m 文件。就像我说的,它会拉回数据,但我需要根据我的 Documents plist 副本动态更改它。

提前致谢!

 @synthesize content = _content;

 - (NSArray *)content
{
    if (!_content) {
        _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
    }
    return _content;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.content count];
}

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

    // Configure the cell...
    cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"Name"];
    cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"Score"];

    return cell;
}

【问题讨论】:

  • 此代码从资源包中读取。从 Documents 文件夹中读取数据的代码在哪里?您是否有将初始文件放入 Documents 文件夹的代码?

标签: ios objective-c uitableview plist


【解决方案1】:

试试这个:

第一步:在.h文件中声明一个全局变量

@property (nonatomic,retain) NSMutableDictionary *contents;

第2步:在.m文件集合中合成

@synthesize contents;

第 3 步:将 plist 文件从 ma​​inbudle 移动到文档目录

-(void) createPlistDocuments
{
    // Get path to documents directory
    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // Finds the contained Documents directory
    NSString *documentsDirectory = [arrayPaths objectAtIndex:0];
    NSError *error;
    // Create an object that we will later use to look for a file and return a boolean value on whether or not it exists
    NSFileManager *manager = [NSFileManager defaultManager];
    // File we want to move, stored in original top level directory
    NSString *demoFile = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    // Define where we want it moved to and name it
    NSString *demoFileMoved = [NSString stringWithFormat:@"%@/data.plist", documentsDirectory];
    // Attempt the copy
    if ([manager copyItemAtPath:demoFile toPath:demoFileMoved error:&error] != YES)
    NSLog(@"Unable to move file: %@", [error localizedDescription]);
}

第四步:文档目录

读取plist数据
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createPlistDocuments];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@/data.plist",documentsDirectoryPath];
    contents = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%d", contents.count);

}

有关简要说明,请遵循此示例...TechDevMobile(IOS-Message-Chat)

【讨论】:

  • Rajesh - 感谢您提供详细示例。此表将返回,但没有任何内容。我添加了以下 NSLog: NSLog(@"%@", documentsDirectory);这让我获得了正在创建/使用的 plist 文件的正确路径。当我打开它时,它的 xml 格式不正确。见下文:bplist00¢“UScoreTNameR10XJohn Doe”R20YBilly Bob',/ 9 这就是我的 Xcode 中原始 plist 的形成方式:NameJohn Doe Score10 等等...
  • 只是跟进,它似乎正在将文件写入文档目录,只是不是我期望的格式,也不是我可以在表格视图中成功读取它的位置。有什么想法吗?
  • 还有更多疑问.. 只需按照此示例阅读 plist github.com/Tech-Dev-Mobile/IOS-Message-Chat
  • 现在使用上面的代码..我做了一些更改..首先你需要将主包文件移动到文档目录然后只有你可以从文档目录中获取文件。如果您通过在文档目录中粘贴来创建 plist 文件意味着只使用 viewdidload 代码..
  • 谢谢拉杰什!看来我已经准备好了!
猜你喜欢
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 2013-06-28
  • 2016-09-14
相关资源
最近更新 更多