UITableView(不使用导航模板)从 plist 获取特定数据
我正在使用《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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我写了一个示例代码,这是一个地址簿。它从 plist 中读取数据。
plist:
读取plist:
在表格视图中显示联系人:
I wrote an example code, that is an address book. It reads the data from a plist.
the plist:
read the plist:
display contacts in tableview: