UITableView(不使用导航模板)从 plist 获取特定数据

发布于 2024-10-05 11:15:43 字数 2031 浏览 0 评论 0原文

我正在使用《Beginning iPhone Development (Exploring the iPhone SDK)》一书第 210 页上的示例,它与我想要做的类似,但由于使用 TableView 中的部分,该特定示例变得复杂。我的 plist 中有一个特定的层次结构......

Root  ---- Dictionary
        Rows  ---- Array
                Item 0- Dictionary
                        fullName  ---- String
                        address   ---- String
                Item 1   ---- Dictionary
                        fullName  ---- String
                        address   ---- String

所以我有一个 UITableView 占据了该“屏幕”上视图的一小部分。视图的其余部分有其他元素,所以我选择 nut 来使用导航模板。

我使用的代码不匹配,因为我真的不清楚调用哪些字段。

有人可以向我展示一个非常简单的示例,如何列出该表中的所有“firstNames”。如果我的 plist 有问题,请让我具体知道要更改的内容。

简而言之,我想循环遍历所有 Item # 字典以列出所有名字。我的设计类似于联系人列表,但不完全是联系人列表。

现在我正在使用这段代码,它只显示单词“Rows”,我将单词 rows 更改为 plist 中的 Rows1,并且它显示出来,因此它正在抓取该“数组项”。我希望我说的是对的。

-(void)viewDidLoad {    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.names = dict;
    [dict release];

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.listData = array;

    [super viewDidLoad];
}

 

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

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"Identifier";

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

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    return cell;
}

我在网络上搜索了好几天,试图找到一个简单的示例,该示例使用 plist 层次结构来列出不属于导航模板的表中的项目。

非常感谢

I'm using the example on page 210 of Beginning iPhone Development (Exploring the iPhone SDK) book and it's similar to what I want to do but that specific example is complicated by using sections in the TableView. I have a specific hierarchy in my plist...

Root  ---- Dictionary
        Rows  ---- Array
                Item 0- Dictionary
                        fullName  ---- String
                        address   ---- String
                Item 1   ---- Dictionary
                        fullName  ---- String
                        address   ---- String

So I have a UITableView that takes up a small portion of the view on that "screen" The rest of the view has other elements so I chose nut to use a navigation template.

The code I'm using doesn't match up because I'm really not clear on calling which fields.

Can someone show me a VERY simple example how I could list all the "firstNames" in that table. If something is wrong with my plist please let me know specifically what to change.

In a nutshell I want to loop through all the Item # dictionaries to list all the first names. My design is similar to a contact list, but not exactly a contact list.

Right now I'm using this code which simply displays the word "Rows" I changed the word rows to Rows1 in my plist and that shows up so it's grabbing that "array item". I hope I said that right.

-(void)viewDidLoad {    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.names = dict;
    [dict release];

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.listData = array;

    [super viewDidLoad];
}

 

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

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"Identifier";

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

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    return cell;
}

I've scoured the web for days trying to find a simple example that uses a plist hierarchy to list items in a table that is not part of a navigation template.

Thanks So Much

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

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

发布评论

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

评论(1

痞味浪人 2024-10-12 11:15:43

我写了一个示例代码,这是一个地址簿。它从 plist 中读取数据。

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>name</key>
        <string>Vikingo</string>
        <key>familyname</key>
        <string>Segundo</string>
        <key>street</key>
        <string>Avenida Roca y Coranado</string>
        <key>number</key>
        <integer>20</integer>
        <key>city</key>
        <string>Santa Cruz de la Sierra</string>
        <key>province</key>
        <string>Santa Cruz</string>
        <key>country</key>
        <string>Bolivia</string>
        <key>pictureurl</key>
        <string>vikingosegundo.png</string>
    </dict>
    <dict>
        <key>name</key>
        <string>Santa</string>
        <key>familyname</key>
        <string>Claus</string>
        <key>street</key>
        <string>Avenida Roca y Coranado</string>
        <key>number</key>
        <integer>20</integer>
        <key>city</key>
        <string>Santa Cruz de la Sierra</string>
        <key>province</key>
        <string>Santa Cruz</string>
        <key>country</key>
        <string>Finland</string>
        <key>pictureurl</key>
        <string>robot-santa.png</string>
    </dict>
</array>
</plist>

读取plist:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
contacts = [[NSArray arrayWithContentsOfFile:plistPath] retain];

在表格视图中显示联系人:

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [contacts count];
}


// Customize the appearance of table view cells.
- (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];
    }

    NSDictionary *dict = [contacts objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [dict objectForKey:@"name"], [dict objectForKey:@"familyname"]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    DetailContactViewController *detailViewController = [[DetailContactViewController alloc] initWithNibName:@"DetailContactView" bundle:nil];
    detailViewController.contact = [contacts objectAtIndex:indexPath.row];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];

    [detailViewController release];

}

I wrote an example code, that is an address book. It reads the data from a plist.

the 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>name</key>
        <string>Vikingo</string>
        <key>familyname</key>
        <string>Segundo</string>
        <key>street</key>
        <string>Avenida Roca y Coranado</string>
        <key>number</key>
        <integer>20</integer>
        <key>city</key>
        <string>Santa Cruz de la Sierra</string>
        <key>province</key>
        <string>Santa Cruz</string>
        <key>country</key>
        <string>Bolivia</string>
        <key>pictureurl</key>
        <string>vikingosegundo.png</string>
    </dict>
    <dict>
        <key>name</key>
        <string>Santa</string>
        <key>familyname</key>
        <string>Claus</string>
        <key>street</key>
        <string>Avenida Roca y Coranado</string>
        <key>number</key>
        <integer>20</integer>
        <key>city</key>
        <string>Santa Cruz de la Sierra</string>
        <key>province</key>
        <string>Santa Cruz</string>
        <key>country</key>
        <string>Finland</string>
        <key>pictureurl</key>
        <string>robot-santa.png</string>
    </dict>
</array>
</plist>

read the plist:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
contacts = [[NSArray arrayWithContentsOfFile:plistPath] retain];

display contacts in tableview:

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [contacts count];
}


// Customize the appearance of table view cells.
- (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];
    }

    NSDictionary *dict = [contacts objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [dict objectForKey:@"name"], [dict objectForKey:@"familyname"]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    DetailContactViewController *detailViewController = [[DetailContactViewController alloc] initWithNibName:@"DetailContactView" bundle:nil];
    detailViewController.contact = [contacts objectAtIndex:indexPath.row];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];

    [detailViewController release];

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