如何将TableView连接到DetailView [英] How do I connect my TableView to a DetailView
问题描述
我的问题是,当用户按下田纳西州啤酒花时,当他们按下我想要推送到列出该区域规格的详细视图时......所以从应用程序的开头它将去田纳西州(RootTable) - >田纳西州啤酒花(SecondTable) - >规格(的DetailView)。有人能指导我如何使其正常运作吗?
谢谢!
RootTableViewController.h
My question is ay when the user presses "Tennessee Hops", when they press that I want to it to push to a detail view listing the specs of that area... So from the opening of the app it would go Tennessee(RootTable) -> Tennessee Hops(SecondTable) -> Specs(DetailView). Can someone lead me in the right direction as to how to get this to function correctly?
Thanks!
RootTableViewController.h
#import <UIKit/UIKit.h>
@interface RootTableViewController : UITableViewController
@end
RootTableViewController.m
RootTableViewController.m
#import "RootTableViewController.h"
#import "SecondTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
{
NSMutableArray *states;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSMutableArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"StatesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [states objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"showArrayDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
SecondTableViewController *destViewController = segue.destinationViewController;
destViewController.stateName = [states objectAtIndex:indexPath.row];
destViewController.title = destViewController.stateName;
}
}
@end
SecondTableViewController.h
SecondTableViewController.h
#import <UIKit/UIKit.h>
@interface SecondTableViewController : UITableViewController
@property (nonatomic, strong) NSString *stateName;
@property (nonatomic, strong) NSString *areaName;
@end
SecondTableViewController.m
SecondTableViewController.m
#import "SecondTableViewController.h"
@interface SecondTableViewController ()
@end
@implementation SecondTableViewController
{
NSMutableArray *alabama;
NSMutableArray *georgia;
NSMutableArray *tennessee;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
alabama = [NSMutableArray arrayWithObjects:@"Moss Rock Preserve Boulder Fields", nil];
georgia = [NSMutableArray arrayWithObjects:@"Georgia Pines", nil];
tennessee = [NSMutableArray arrayWithObjects:@"Tennessee Hops", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([_stateName isEqualToString:@"Alabama"])
{
return [alabama count];
}
else if([_stateName isEqualToString:@"Georgia"])
{
return [georgia count];
}
else if([_stateName isEqualToString:@"Tennessee"])
{
return [tennessee count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Animal2Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if([_stateName isEqualToString:@"Alabama"])
{
cell.textLabel.text = [alabama objectAtIndex:indexPath.row];
}
else if([_stateName isEqualToString:@"Georgia"])
{
cell.textLabel.text = [georgia objectAtIndex:indexPath.row];
}
else if([_stateName isEqualToString:@"Tennessee"])
{
cell.textLabel.text = [tennessee objectAtIndex:indexPath.row];
}
return cell;
}
推荐答案
这篇关于如何将TableView连接到DetailView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!