- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用 uitableviews 深入查看 plist 以获取特定学校。向下钻取状态->地区->学校。我创建了一个 plist,但我不能 100% 确定该结构是最好的。此外,我可以在第一个 tableview 上获得第一组事件信息,但我不确定如何从那里继续。我是否需要为每个向下钻取(stateview、districtview、schoolview)创建一个 TableView ,或者我可以重用一个通用的 TableView ,因为它们将简单地是列表?以下是我到目前为止所拥有的。感谢您的帮助。
PLIST
<plist version="1.0">
<array>
<dict>
<key>districts</key>
<dict>
<key>District 1</key>
<array>
<string>School 2</string>
<string>School 1</string>
</array>
<key>District 2</key>
<array>
<string>School 3</string>
<string>School 4</string>
</array>
</dict>
<key>state</key>
<string>South Dakota</string>
</dict>
<dict>
<key>districts</key>
<array>
<string>District 1</string>
<string>District 2</string>
</array>
<key>state</key>
<string>Arkansas</string>
</dict>
<dict>
<key>districts</key>
<array>
<string>District 3</string>
<string>District 4</string>
</array>
<key>state</key>
<string>New York</string>
</dict>
</array>
</plist>
这是我的 View Controller
#import "plistViewController.h"
@interface plistViewController ()
@end
@implementation plistViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
@synthesize content = _content;
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"state"];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
}
@end
最佳答案
UITableView 最好的地方在于它不关心显示什么数据。它只是询问它的委托(delegate)
几个不同的问题:
因此,您必须专注于让您的委托(delegate)响应提供正确的数据。
因此,首先将您的 plist 分成可管理的 block 。 UITableView prima-donna 数据源是一个 NSArray。由于索引逻辑,可以整齐地映射到 tableView。
也就是说,您的第一个 tableViewController plistViewController
具有良好的显示信息逻辑。具体来说,您在数组位置 x
处查询 NSDictionary 并要求它返回其 state
对象。 3 个字典对象,返回 3 个字符串。不错。
那么如何进入下一个级别呢?你的 tableView 会在这里帮助你。它向其 delegate
询问一个特定问题:
您将需要设置另一个名为 DistrictViewController
的 UITableViewController 子类。在 header .h
文件中,您需要为 NSDictionary
对象创建一个 strong
属性。像这样:
//DistrictViewController.h
@interface DistrictViewController : UITableViewController
@property (nonatomic, strong) NSDictionary *districtDictionary;
@end
//DistrictViewController.m
@implementation DistrictViewController
@synthesize districtDictionary;
我们已经有了。此类现在设置为跟踪 1 个 NSDictionary 对象。现在您只需配置您的表委托(delegate)方法来显示您想要的数据。
第一个示例,NSArray 的顶行 (index:0) 中的内容,您有一个包含 2 个键的字典:District 1
和 District 2
。但这是一个问题。 NSDictionary 不能很容易地映射到 TableView,因为 NSDictionary 对象不使用索引来工作。别担心。 NSDictionary 有一个名为 allKeys
的方法,它将为您提供字典中每个键的数组。当您将从某处接收 NSDictionary 但事先不知道它包含什么键时,这很有用。
那么,让我们回答您的 tableView 提出的问题:
//How many sections will be in me: Let's just say 1 for now.
//How many rows will be in this section:
//Ask the NSDictionary how many keys it has:
NSArray *keyArray = [self.districtDictionary allKeys];
return [keyArray count];
//Give me a tableCell for index path X,Y
//First, get your array of keys back:
NSArray *keyArray = [self.districtDictionary allKeys];
//Next, find the key for the given table index:
NSString *myKey = [keyArray objectAtIndex:indexPath.row];
//Finally, display this string in your cell:
cell.textLabel.text = myKey;
在此之后,您将为最终 View 执行相同的操作。为学校设置一个 viewController 并将其命名为 SchoolViewController
并将其设置为负责 NSArray。就像以前一样:
@interface SchoolViewController : UITableViewController
@property (nonatomic, strong) NSArray *schoolArray;
@end
@implementation SchoolViewController
@synthesize schoolArray;
在这个 View 中,它会很像第一个。你只需让这个 viewController 像以前一样回答表格的问题:
return [schoolArray count];
cell.textLabel.text = [schoolArray objectAtIndex:indexPath.row];
将所有这些放在一起的最后一部分是表格提出的最后一个问题。
在每个 View 中,查看此方法签名:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
这是您添加逻辑以连接事物的地方。在第一个 View plistViewController
中,执行以下操作:
NSDictionary *topLevelDictionary = [self.content objectAtIndex:indexPath.row];
NSDictionary *allDistricts = [topLevelDictionary objectForKey:@"districts"];
DistrictViewController *dView = [[DistrictViewController alloc] initWithStyle:UITableViewStylePlain];
dView.districtDictionary = allDistricts;
[self.navigationController pushViewController:dView animated:YES];
在第二个 View 中,DistrictViewController
这样做:
NSArray *keyArray = [self.districtDictionary allKeys];
NSString *myKey = [keyArray objectAtIndex:indexPath.row];
NSArray *schoolArray = [self.districtDictionary objectForKey:myKey];
SchoolViewController *sView = [[SchoolViewController alloc]initWithStyle:UITableViewStylePlain];
sView.schoolArray = schoolArray;
[self.navigationController pushViewController:sView animated:YES];
希望对您有所帮助。我在纯文本编辑器中输入了所有内容。希望没有拼写错误。您需要在每个 View Controller 中 #import 关联的 View Controller !祝你好运。
关于ios - 如何使用 UITableViews 向下钻取 plist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18321438/
运行 apache-drill-1.14.0 在 mysql 上测试这个查询并在 0.02 秒内得到结果 在 drill cli 中运行查询失败并出现 AssertionError 查询: produ
我是 Apache Drill 的新手。 场景是这样的: 我有一个 S3 存储桶,我在其中放置了名为 test.csv 的 csv 文件。 我已经按照官方网站的说明安装了 Apache Drill。
我正忙于学习第 2 版《编程:使用 C++ 的原理与实践》(Stroustrup),并且在读取值时遇到了有无空格的问题,然后再次显示它们。 能否请您指出正确的方向,以便我找出结果不同的原因? 代码编译
我是一名优秀的程序员,十分优秀!