【问题标题】:iOS load data from a .plist fileiOS 从 .plist 文件加载数据
【发布时间】:2014-01-06 15:15:51
【问题描述】:

我是 iOS 开发的新手。目前,我正在尝试从 .plist 文件中获取数据并显示在 UITableView 上。运行应用程序时,Xcode 没有提供任何错误,但我的应用程序只显示这个

这是我的代码

TestAppTableViewController.h

#import <UIKit/UIKit.h>

@interface TestAppTableViewController : UITableViewController

@property (nonatomic,strong) NSArray *content;

@end 

TestAppTableViewController.m

#import "TestAppTableViewController.h"

@interface TestAppTableViewController ()

@end

@implementation TestAppTableViewController

@synthesize content = _content;

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

    return _content;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 0;
}

- (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:@"city"];
    cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row]valueForKey:@"state"];
    return cell;
}

数据.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>city</key>
        <string>New York</string>
        <key>state</key>
        <string>NY</string>
        <key>cityText</key>
        <string>This is the description of the city that goes in the tex view just testing.</string>
    </dict>
    <dict>
        <key>city</key>
        <string>Los Angeles</string>
        <key>state</key>
        <string>CA</string>
        <key>cityText</key>
        <string>This the second item&apos;s textview</string>
    </dict>
    <dict>
        <key>city</key>
        <string>Chicago</string>
        <key>state</key>
        <string>IL</string>
        <key>cityText</key>
        <string>here is the text view description for the third item.</string>
    </dict>
    <dict>
        <key>city</key>
        <string>Sandiago</string>
        <key>state</key>
        <string>CA</string>
        <key>cityText</key>
        <string>Here is another city from california</string>
    </dict>
    <dict>
        <key>city</key>
        <string>Talahasy</string>
        <key>state</key>
        <string>FL</string>
        <key>cityText</key>
        <string>Talahasy is the capital of the florida state.</string>
    </dict>
    <dict>
        <key>city</key>
        <string>Boise</string>
        <key>state</key>
        <string>Idaho</string>
        <key>cityText</key>
        <string>boise is the capital of idaho and i have no idea where that is.</string>
    </dict>
</array>
</plist>

我无法找出问题所在。所以,我的问题是我的代码有什么问题?如何显示 plist 文件中的数据?我浏览了 stackoverflow 中的上一个问题,但这对我没有帮助。

【问题讨论】:

  • numberOfSectionsInTableView 你返回 0 吗?
  • @Yasir 您需要解析 plist 数据,然后将其加载到 tableview 中。检查您的阵列是否有信息一次。使用 Json 解析器

标签: ios iphone objective-c uitableview plist


【解决方案1】:

您的 tableview 至少需要一个部分-

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {

    // Return the number of sections.
    return 1;// make this 1
}

也可以尝试像这样使用 objectForKey(尽管根据文档,如果 key 不以 @ 开头,则会自动调用它)并进行类型转换以确保。

cell.textLabel.text = (NSString*)[[self.content objectAtIndex:indexPath.row] objectForKey:@"city"];
cell.detailTextLabel.text = (NSString*)[[self.content objectAtIndex:indexPath.row] objectForKey:@"state"];

如果您仍然一无所获,那么值得检查内容是否实际包含 plist 数据。在您的 -(NSArray *)content 方法中,在您返回之前,可能值得记录 _content 的计数。同时确认 self.content = _content

【讨论】:

    【解决方案2】:

    我认为问题出在这里,你应该返回 1 或者你想要的除 0 以外的任何数字:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    
        // Return the number of sections.
        return 0;
    }
    

    【讨论】:

      【解决方案3】:
      @interface ViewController:UIViewController <UITableViewDelegate, UITableDataSource>
      

      只需查看您在数组中的 plist,您就可以使用字典。所以像下面的代码那样实现它可以帮助你

      - (void)viewDidLoad {
      
          tableView.delegate = self;
          tableView.dataSource = self;
      
          NSString *path = [[NSBundle mainBundle] pathForResource:@"yourFilename" ofType:@"plist"];
          NSArray *contentArray = [NSArray arrayWithContentsOfFile:path];
          // Having outlet for tableArray variable.
          tableArray = [[NSMutableArray alloc]initWithArray:contentArray copyItems:YES];
      
          [super viewDidLoad];
      
      }
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
          return [tableArray count];
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         // In your case dictionary contains strings with keys and values. The below line returns dictionary only. not array..
          NSDictionary *dictionary = [tableArray objectAtIndex:section];
          return dictionary.allKeys.count;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          static NSString *cellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
          }
          NSDictionary *dictionaryOfArray = [tableArray objectAtIndex:indexPath.section];
          NSArray *keysArray = dictionaryOfArray.allKeys;
          cell.textLabel.text = [keysArray objectAtIndex:indexPath.row];
          cell.detailTextLabel.text = [dictionaryOfArray valueForKey:[keysArray objectAtIndex:indexPath.row]];
          return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-06
        • 2012-07-19
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多