Проблемы с получением заголовка ячейки из PList в UITableView - PullRequest
1 голос
/ 28 марта 2011

просто очень глупая проблема с чтением PList и настройкой cell.textLabel.text в контроллере tableView.

Мой 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>Rows</key>
        <array>
            <dict>
                <key>Name</key>
                <string>Big Ben</string>
            </dict>
            <dict>
                <key>Name</key>
                <string>Westminster Abbey</string>
            </dict>
        </array>
        <key>Title</key>
        <string>London</string>
    </dict>
    <dict>
        <key>Rows</key>
        <array>
            <dict>
                <key>Name</key>
                <string>Eiffel Tower</string>
            </dict>
        </array>
        <key>Title</key>
        <string>Paris</string>
    </dict>
</array>
</plist>

И япросто пытаюсь получить имя в клетку.Это то, что у меня есть софар.

cell.textLabel.text = [[[[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"]  objectAtIndex: indexPath.section] objectForKey: @"Name"] objectAtIndex: indexPath.row];

Заранее спасибо.

Ответы [ 2 ]

1 голос
/ 30 декабря 2012

Попробуйте это:

.m

#import "plistTableViewController.h"
#import "DetailViewController.h"
@interface plistTableViewController ()

@end

@implementation plistTableViewController


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

- (void)viewDidLoad
{

    NSString *mylist = [[NSBundle mainBundle] pathForResource:@"lodges" ofType:@"plist"];
    tabledata = [[NSArray alloc]initWithContentsOfFile:mylist];
    NSLog(@"%@", tabledata);
    [super viewDidLoad];

   }

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



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

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

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

    // Return the number of rows in the section.
    return [tabledata count];
}

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

    if( cell == nil )
    {
        NSLog(@"Cell Creation");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    //Set Data For each Cell
    cell.textLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellName"];
    cell.detailTextLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellSubtitle"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

.h

#import <UIKit/UIKit.h>

@interface plistTableViewController : UITableViewController
{
    NSArray *tabledata;
}

@end

мой список имен называется lodges.plist, просто измените информацию по мере необходимости, и она будет работать. Я также добавил субтитры для вас.

1 голос
/ 29 марта 2011

Спасибо @aBitObious за ваш совет - получил сортировку. Ответ:

NSDictionary *dictionary = [[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"];
cell.textLabel.text = [[dictionary objectAtIndex: indexPath.row] objectForKey: @"Name"];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...