1

Storyboard を使用する iPhone アプリケーションを開発しています。最初のシーンには、Table View Controller があります。会社という名前のプロパティ ファイルがあり、そのファイルの値を読み込んで UITableView に表示しようとしています。私は何時間もこれに取り組んできましたが、役に立ちませんでした。この質問は近づいてきましたが、その答えも役に立ちませんでした。

プロパティ ファイルの形式は次のとおりです。

ここに画像の説明を入力

これが私のコードです。

CompaniesTableViewController.mファイル

#import "CompaniesTableViewController.h"

@interface CompaniesTableViewController ()

@end

@implementation CompaniesTableViewController

NSMutableArray *companies;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
    companies = [[NSMutableArray alloc]initWithContentsOfFile:companiesFile];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [companies count];
}

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

    cell.textLabel.text = [[companies objectAtIndex:indexPath.row]objectForKey:@"Value"];

    return cell;
}

テーブル ビューにはデータが取り込まれません。私が見落としているもの、ここで欠けているものを誰か教えてもらえますか?

4

7 に答える 7

1

まず、割り当てて初期化する必要がありますUITableViewCell

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

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }
    cell.textLabel.text = [[companies objectAtIndex:indexPath.row]objectForKey:@"Value"];

    return cell;
}
于 2012-12-07T11:50:07.787 に答える
0
    NSString *path = [[NSBundle mainBundle] pathForResource:@"companies" ofType:@"plist"];
    NSData *plistData = [NSData dataWithContentsOfFile:path];
    NSString *error; NSPropertyListFormat format;
    NSArray *companyData = [NSPropertyListSerialization propertyListFromData:plistData
                                                                      mutabilityOption:NSPropertyListImmutable
                                                                                format:&format
                                                                      errorDescription:&error];

  companies= [[NSMutableArray alloc] initWithArray:companyData];
    NSLog(@"companyData:%@",companyData);
于 2012-12-07T12:08:36.407 に答える
0

こんにちはまず、plistファイルの構造は次のようにする必要があります(一部のみが表示されています)

ここに画像の説明を入力してください

それから私が置いた場所は(社長はルートフォルダです)

ここに画像の説明を入力してください

次に、データを読み取るために使用したコードは(大統領がいる場所NSArray)です。

ここに画像の説明を入力してください

于 2012-12-07T12:31:02.220 に答える
0

その種のplistの場合..NSDictonaryを作成する必要があります..

@implementation CompaniesTableViewController

    {
    NSDictionary *dict;
    NSArray *arayDict;
    }

..

NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
//populate dicationary
dict = [[NSDictionary alloc] initWithContentsOfFile:companiesFile];
//populate array with allkeys from dict
arayDict = [dict allKeys];

-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayDict count];
}

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

    NSString *key = [arayDict objectAtIndex:indexPath.row];
    NSDictionary *dictionary = [dict objectForKey:key];
    cell.textLabel.text =[NSString stringWithFormat:@"%@", dictionary];
    return cell;
}
于 2012-12-07T14:10:32.987 に答える
0

データを読み込んだ後、テーブルビューをリロードする必要があります。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *companiesFile = [[NSBundle mainBundle]pathForResource:@"companies" ofType:@"plist"];
    companies = [[NSMutableArray alloc]initWithContentsOfFile:companiesFile];

    [self.tableView reloadData];
}
于 2012-12-07T13:16:14.840 に答える
0

pList のエントリ タイプは何ですか。pList から会社名を取得できますか。会社の値キーを使用して会社名を NSLogging してみてください。

于 2012-12-07T12:00:21.420 に答える
0

理想的には、plist には会社として 1 つのキーを持つ辞書があり、その値は NSArray である必要があります。

配列ではなく辞書を読み取ります。

于 2012-12-07T11:51:26.533 に答える