Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To create drill down tables:</p> <p><img src="https://i.stack.imgur.com/ysdgi.png" alt="Boys and Girls example"></p> <p>you can do:</p> <pre><code>- (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]; } </code></pre> <p>Next:</p> <pre><code>- (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; } </code></pre> <p>Next:</p> <pre><code>- (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; } </code></pre> <p>Next:</p> <pre><code>- (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&lt;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]; } </code></pre> <p>And you can customize this table, like you need. Example of drill down tables you can download <a href="http://yadi.sk/d/Dg-3iMhc85oE6" rel="nofollow noreferrer">here</a> (click "Скачать" button)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload