我使用的是《开始iPhone开发(探索iPhone SDK)》一书第210页上的示例,它与我想要做的相似,但是通过使用TableView中的部分,该特定示例变得很复杂。我的清单中有特定的层次结构...
Root ---- Dictionary
Rows ---- Array
Item 0- Dictionary
fullName ---- String
address ---- String
Item 1 ---- Dictionary
fullName ---- String
address ---- String
因此,我有一个UITableView占据了该“屏幕”上一小部分视图。视图的其余部分还有其他元素,因此我选择了使用导航模板。
我使用的代码不匹配,因为我不清楚在调用哪些字段。
有人可以给我展示一个非常简单的示例,我如何列出该表中的所有“名字”。如果我的列表有问题,请具体告诉我要更改的内容。
简而言之,我想遍历所有Item#字典以列出所有名字。我的设计类似于联系人列表,但不完全是联系人列表。
现在,我正在使用此代码,该代码仅显示单词“ Rows”,我在plist中将单词rows更改为Rows1并显示出来,因此它可以抓取“ array item”。我希望我说的对。
-(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层次结构列出不属于导航模板的表中的项目。
非常感谢
最佳答案
我写了example code,这是一个通讯录。它从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>
阅读清单:
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];
}