2

我想使用 uitableviews 深入了解 plist 以获得特定的学校。向下钻取是州->区->学校。我创建了一个 plist,但不能 100% 确定该结构是最好的。此外,我可以在第一个 tableview 上获得第一组活动信息,但不知道如何从那里开始。我是否需要为每个向下钻取(stateview、districtview、schoolview)创建一个表格视图,或者我可以重用一个通用表格视图,因为它们只是列表?以下是我到目前为止所拥有的。谢谢你的帮助。

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>

这是我的视图控制器

#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
4

3 回答 3

2

UITableView 最好的一点是它不关心它显示什么数据。它只是问delegate了几个不同的问题:

  1. 我应该有多少个部分?
  2. 每个部分有多少行?
  3. 我可以为这个_索引路径提供一个 UITableViewCell 吗?

因此,您必须专注于让您的委托响应提供正确的数据。

因此,首先将您的 plist 拆分为可管理的块。UITableView prima-donna 数据源是一个 NSArray。由于索引逻辑,整齐地映射到 tableViews。

也就是说,您的第一个 tableViewControllerplistViewController具有显示信息的良好逻辑。具体来说,您在数组位置查询 NSDictionaryx并要求它返回其state对象。3 个字典对象,返回 3 个字符串。好的。

那么如何进入下一个层次呢?您的 tableView 将在这里为您提供帮助。它提出了一个特定的问题delegate

  1. 当用户触摸 Section Y Row X 时我该怎么办?

您将需要设置另一个 UITableViewController 子类,称为DistrictViewController. 在头.h文件中,您将需要为对象创建一个strong属性NSDictionary。像这样:

//DistrictViewController.h
@interface DistrictViewController : UITableViewController
@property (nonatomic, strong) NSDictionary *districtDictionary;
@end

//DistrictViewController.m
@implementation DistrictViewController
@synthesize districtDictionary;

我们终于得到它了。此类现在设置为跟踪 1 个 NSDictionary 对象。现在你只需要配置你的表委托方法来显示你想要的数据。

第一个示例,NSArray 的第一行(索引:0)中的内容,您有一个包含 2 个键的字典:District 1District 2. 但这是一个问题。NSDictionary 并不那么容易映射到 TableViews,因为 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;

在此之后,您将对最终视图执行相同的操作。为学校设置一个viewController并调用它SchoolViewController并设置它来负责一个NSArray。就像从前一样:

@interface SchoolViewController : UITableViewController
@property (nonatomic, strong) NSArray *schoolArray;
@end

@implementation SchoolViewController
@synthesize schoolArray;

从这个角度来看,它会很像第一个。您只需让这个 viewController 像以前一样回答表格的问题:

  1. 几节?我们需要 1
  2. 多少行?我们需要和数组一样多return [schoolArray count];
  3. 给我一个细胞:cell.textLabel.text = [schoolArray objectAtIndex:indexPath.row];

将这一切放在一起的最后一块是表格提出的最后一个问题。

  1. 当用户触摸一行时我该怎么办?

在每个视图中,查看此方法签名:

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

这是您添加逻辑以连接事物的地方。在第一个视图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];

在第二个视图中,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];

我希望这可以帮助你。我在纯文本编辑器中输入了这一切。希望没有拼写错误。您需要在每个视图控制器中#import 关联的视图控制器!祝你好运。

于 2013-08-19T20:17:20.483 回答
1

要创建向下钻取表:

男孩和女孩的例子

你可以做:

- (void)viewDidLoad
{
[super viewDidLoad];


NSArray *districts = [NSArray arrayWithObjects:@"district1", @"district2", @"district3", nil];
NSArray *states = [NSArray arrayWithObjects:@"NY", @"NJ", @"NO", @"StateOther1", @"StateOther2", nil];
NSArray *schools = [NSArray arrayWithObjects:@"", @"school1", @"school2", @"school3", @"school4", nil];

NSMutableDictionary *schoolSection = [NSMutableDictionary dictionary];
[schoolSection schools forKey:@"items"];
[schoolSection setObject:@"Shools" forKey:@"title"];

NSMutableDictionary *districtSection = [NSMutableDictionary dictionary];
[districtSection setObject:districts forKey:@"items"];
[districtSection setObject:@"Section" forKey:@"title"];

NSMutableDictionary *stateSection = [NSMutableDictionary dictionary];
[districtSection setObject:states forKey:@"items"];
[districtSection setObject:@"State" forKey:@"title"];

self.adresses = @[schoolSection, districtSection,stateSection];
}

下一个:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.adresses.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSection = [self.adresses objectAtIndex:section];
if ([[currentSection objectForKey:@"isOpen"] boolValue]) {
    NSArray *items = [currentSection objectForKey:@"items"];
    return items.count;
}
return 0;
}

- (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];
}

NSDictionary *currentSection = [self.adresses objectAtIndex:indexPath.section];
NSArray *items = [currentSection objectForKey:@"items"];
NSString *currentItem = [items objectAtIndex:indexPath.row];
cell.textLabel.text = currentItem;

return cell;
}

下一个:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSDictionary *currentSection = [self.adresses objectAtIndex:section];
NSString *sectionTitle = [currentSection objectForKey:@"title"];
BOOL isOpen = [[currentSection objectForKey:@"isOpen"] boolValue];
NSString *arrowNmae = isOpen? @"arrowUp":@"arrowDown";

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, 320.0f, 50.0f);
button.tag = section;
button.backgroundColor = [UIColor brownColor];
[button setTitle:sectionTitle forState:UIControlStateNormal];
[button addTarget:self action:@selector(didSelectSection:)
 forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:arrowNmae] forState:UIControlStateNormal];
return button;
}

下一个:

- (void)didSelectSection:(UIButton*)sender {
//get current section
NSMutableDictionary *currentSection = [self.adresses objectAtIndex:sender.tag];

//get elements of section
NSArray *items = [currentSection objectForKey:@"items"];

//create array of indexes
NSMutableArray *indexPaths = [NSMutableArray array];
for (int i=0; i<items.count; i++) {
    [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:sender.tag]];
}

//get current state  of section is opened
BOOL isOpen = [[currentSection objectForKey:@"isOpen"] boolValue];

//set new state
[currentSection setObject:[NSNumber numberWithBool:!isOpen] forKey:@"isOpen"];

//animate of adding and deleting of cells
if (isOpen) {
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
} else {
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
}

//reload button image
NSString *arrowNmae = isOpen? @"arrowDown.png":@"arrowUp.png";
[sender setImage:[UIImage imageNamed:arrowNmae] forState:UIControlStateNormal];
}

您可以根据需要自定义此表。可以在此处下载的深入表示例(单击“Скачать”按钮)

于 2013-08-19T19:42:21.977 回答
0

您应该将区域数组传递给可以显示它们的新视图控制器。新的视图控制器应该有一个称为区的属性,我还建议创建一个初始化器,它接受一个设置此属性的区数组。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *districts = [[self.content objectAtIndex:indexPath.row] valueForKey:@"districts"];
    DistrictsViewController *districtsvc =  
     [[DistrictsViewController alloc] initWithNibName:nil 
                                               bundle:nil 
                                            districts:districts];
    [self.navigationController pushViewController:districtsvc];
}

从您的示例中,我不确定学校信息的来源,因此如果很难说您是否能够轻松创建单个通用视图控制器以从州深入到学校。

于 2013-08-19T19:35:15.823 回答