从 UITableView 中的 PList 获取单元格标题的问题

发布于 2024-10-27 14:44:49 字数 1461 浏览 3 评论 0原文

只是在读取 PList 并在 tableView 控制器中设置 cell.textLabel.text 时遇到一个非常愚蠢的问题。

我的 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];

提前致谢。

just having a really silly problem with reading the PList and setting the cell.textLabel.text in a tableView controller.

My PList looks like this:

<?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>

And I am just trying to get the name into the cell. This is what I have sofar.

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-11-03 14:44:49

感谢@aBitObious 的建议 - 已排序。答案是:

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

Thanks to @aBitObious for your advice - got is sorted. Answer is:

NSDictionary *dictionary = [[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"];
cell.textLabel.text = [[dictionary objectAtIndex: indexPath.row] objectForKey: @"Name"];
海未深 2024-11-03 14:44:49

试试这个:

.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

我的 plist 名为 lodges.plist 只需根据需要更改信息即可工作。我还为您添加了字幕。

Try this:

.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

my plist is named lodges.plist just change the information as needed and it will work. I have also added subtitles for you as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文