0

重複の可能性:
PList から UITableView にデータを取得する方法は?

ディクショナリとディクショナリごとの文字列の数を含む plist を持っています。

これらのリストをテーブルビューに表示したい

画像を入力してください .

これplistをどのように表示できますかUITableView

私がしようとしているのは:

- (id)readPlist:(NSString *)fileName 
{

NSString *error;
NSPropertyListFormat format;
id plist;

NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];

dic =[NSDictionary dictionaryWithContentsOfFile:localizedPath];



plist = [NSPropertyListSerialization propertyListFromData:dic mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
    NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
    [error release];
}

return plist;
}


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

dict =[self readPlist:@"A"];
return dict.allKeys.count;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {

dict = [self readPlist:@"A"];
 key = [dict.allKeys objectAtIndex:section];
return [[dict valueForKey:key] count];

}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

  cell.textLabel.text = [[dict objectForKey:key] objectAtIndex:indexPath.row];

return cell;
}
4

2 に答える 2

1

Plistデータを配列に格納する

- (id)readPlist:(NSString *)fileName 
{

NSString *error;
NSPropertyListFormat format;
id plist;

NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];

// declare your array in .h file
  array = [NSArray arrayWithContentsOfFile:localizedPath];    

plist = [NSPropertyListSerialization propertyListFromData:dic mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
    NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
    [error release];
}

return plist;
}

そしてそれをテーブルに書きます

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

  cell.textLabel.text = [array objectAtIndex:indexPath.row] valueForKey:@"keyname"];;

return cell;
}
于 2012-12-08T09:51:51.893 に答える
1

更新 2: orにdelegateanddatasourceを設定する必要がありtableViewます。xibViewController

あなたのViewController.hファイルで

@interface ViewController:UIViewController <UITableViewDelegate, UITableDataSource>

私があなたのために書いたこのコードを試してください。

- (void)viewDidLoad {

    tableView.delegate = self;
    tableView.dataSource = self;

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:path];
    // Having outlet for tableArray variable.
    tableArray = [[NSMutableArray alloc]initWithArray:contentArray copyItems:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (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 = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    NSDictionary *dictionary = [tableArray objectAtIndex:indexPath.section];
    NSArray *keysArray = dictionary.allKeys;
    // This below line display the key in textLabel
    cell.textLabel.text = [keysArray objectAtIndex:indexPath.row];
    // Below line will display the value of a key in detailTextLabel.
    cell.detailTextLabel.text = [dictionary valueForKey:[keysArray objectAtIndex:indexPath.row]];
    return cell;
}

更新 2: MAC であなたを確認した後、あなたのplistで作業していることを知りました。array of dictionariesA.plist

ここに画像の説明を入力

bugそのため、コード自体にa があることがわかりました。ファイルにないので、..plistを使用できます8000 data plist too。その作業も。私は完全にチェックアウトしました。これで、上記のコードを取得して作業を開始できます。

于 2012-12-08T10:35:38.197 に答える