简体   繁体   English

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

[英]UITableView (not using Navigation Template) getting specific data from 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. 我正在使用“开始iPhone开发(探索iPhone SDK)”一书的第210页上的示例,它与我想要做的类似,但是通过使用TableView中的部分,这个具体示例很复杂。 I have a specific hierarchy in my plist... 我的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. 所以我有一个UITableView占据了“屏幕”视图的一小部分视图的其余部分有其他元素所以我选择了坚果来使用导航模板。

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. 有人可以给我一个非常简单的例子,我可以列出该表中的所有“firstNames”。 If something is wrong with my plist please let me know specifically what to change. 如果我的plist有问题,请告诉我具体要改变什么。

In a nutshell I want to loop through all the Item # dictionaries to list all the first names. 简而言之,我想循环遍历所有Item#dictionaries以列出所有名字。 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". 现在我正在使用这个代码,它只是显示单词“Rows”我将单词行更改为我的plist中的Rows1,然后显示出它正在抓取“数组项”。 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. 我已经在网上搜了几天试图找到一个简单的例子,该例子使用plist层次结构来列出不属于导航模板的表中的项目。

Thanks So Much 非常感谢

I wrote an example code , that is an address book. 我写了一个示例代码 ,这是一个地址簿。 It reads the data from a plist. 它从plist中读取数据。

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

read the plist: 阅读plist:

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

display contacts in tableview: 在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];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM