我遇到了在目标C中推送到新的TableView的问题。现在我把它带到第一个表中它出现动物列表的位置,你按动物,例如“狗”,它然后推送到新视图并显示阵列中的狗列表。我想能够选择一只特定的狗,它会说出关于那只特定狗的“规格”......我试图对我到目前为止所做的工作进行一些逆向工程,但我没有成功。
现在我有Main.storyboard,RootTableViewController和SecondTableViewController ......
我觉得我需要添加一个ThirdTableViewController来添加我的下一个TableView,它将列出所选狗的规格,但这是我需要添加的唯一类吗?
RootTableViewController.m
#import "RootTableViewController.h"
#import "SecondTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
{
NSArray *animals;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
animals = [NSArray arrayWithObjects:@"Dogs", @"Cats", @"Frogs", @"Gnats", 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 the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [animals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//makes table indentifier
static NSString *simpleTableIdentifier = @"AnimalCell";
//creates the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [animals 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.animalName = [animals objectAtIndex:indexPath.row];
destViewController.title = destViewController.animalName;
}
}
@end
RootTableViewController.h
#import <UIKit/UIKit.h>
@interface RootTableViewController : UITableViewController
@end
SecondTableViewController.m
#import "RootTableViewController.h"
#import "SecondTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
{
NSArray *animals;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
animals = [NSArray arrayWithObjects:@"Dogs", @"Cats", @"Frogs", @"Gnats", 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 the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [animals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//makes table indentifier
static NSString *simpleTableIdentifier = @"AnimalCell";
//creates the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [animals objectAtIndex:indexPath.row];
return cell;
}
SecondTableViewController.h
#import <UIKit/UIKit.h>
@interface SecondTableViewController : UITableViewController
@property(nonatomic, strong) NSString *animalName;
@property (nonatomic) NSInteger *selectedIndex;
-(void)SelectedMasterItem:(NSInteger *)selected;
@end
ThirdTableViewController.m
#import "ThirdTableViewController.h"
@interface ThirdTableViewController ()
@end
@implementation ThirdTableViewController
{
NSArray *dogSpecs;
NSArray *catSpecs;
NSArray *frogSpecs;
NSArray *gnatSpecs;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dogSpecs = [NSArray arrayWithObjects:@"Yellow", @"Black", @"Mean", @"Fast", @"Ugly", nil];
catSpecs = [NSArray arrayWithObjects:@"Smells", @"Hairy", @"Biter", @"Long", nil];
frogSpecs = [NSArray arrayWithObjects:@"Green", @"Yellow", @"Red", nil];
gnatSpecs = [NSArray arrayWithObjects:@"Crash", @"Annoying", @"Sad", 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 the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([_animalSpec isEqualToString:@"Dogs"])
{
return [dogSpecs count];
}
else if([_animalSpec isEqualToString:@"Cats"])
{
return [catSpecs count];
}
else if([_animalSpec isEqualToString:@"Frogs"])
{
return [frogSpecs count];
}
else if([_animalSpec isEqualToString:@"Gnats"])
{
return [gnatSpecs count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//makes table indentifier
static NSString *simpleTableIdentifier = @"Animal3Cell";
//creates the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:simpleTableIdentifier];
}
if([_animalSpec isEqualToString:@"Dogs"])
{
cell.textLabel.text = [dogSpecs objectAtIndex:indexPath.row];
}
else if([_animalSpec isEqualToString:@"Cats"])
{
cell.textLabel.text = [catSpecs objectAtIndex:indexPath.row];
}
else if([_animalSpec isEqualToString:@"Frogs"])
{
cell.textLabel.text = [frogSpecs objectAtIndex:indexPath.row];
}
else if([_animalSpec isEqualToString:@"Gnats"])
{
cell.textLabel.text = [gnatSpecs objectAtIndex:indexPath.row];
}
return cell;
}
@end
ThirdTableViewController.h
#import <UIKit/UIKit.h>
@interface ThirdTableViewController : UITableViewController
@property(nonatomic, strong) NSString *animalSpec;
@end
我很抱歉所有的代码,我只是想发布所有代码,所以也许有人能理解我在说什么。我确信这是一个初学者的事情,我只是不理解,如果是这样,抱歉我的无知。
由于
答案 0 :(得分:0)
> In first ViewController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondTableViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondStoryBoardIdentifier"];
secondViewController.selectedIndex =indexPath.row;
[self.navigationController pushViewController:secondViewController animated:YES];
}
> In Second ViewController
in .h file
@interface SecondTableViewController : UITableViewController
@property (nonatomic) NSInteger *selectedIndex;
-(void)SelectedMasterItem:(NSInteger *)selected;
@end
In .m file
@implementation SecondTableViewController
{
NSArray *dogs;
NSArray *cats;
NSArray *frogs;
NSArray *gnats;
NSMutableArray *arrayItems;
}
- (void)viewDidLoad
{
[super viewDidLoad];
arrayItems = [[NSMutableArray alloc]init];
dogSpecs = [NSArray arrayWithObjects:@"Yellow", @"Black", @"Mean", @"Fast", @"Ugly", nil];
catSpecs = [NSArray arrayWithObjects:@"Smells", @"Hairy", @"Biter", @"Long", nil];
frogSpecs = [NSArray arrayWithObjects:@"Green", @"Yellow", @"Red", nil];
gnatSpecs = [NSArray arrayWithObjects:@"Crash", @"Annoying", @"Sad", nil];
[self SelectedMasterItem:self.selectedIndex];
}
-(void)SelectedMasterItem:(NSInteger *)selected
{
[arrayItems removeAllObjects];
switch (selected) {
case 0:
arrayItems = [dogSpecs mutableCopy];
break;
case 1:
arrayItems = [catSpecs mutableCopy];
break;
case 2:
arrayItems = [frogSpecs mutableCopy];
break;
case 3:
arrayItems = [gnatSpecs mutableCopy];
break;
default:
break;
}
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return arrayItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//makes table indentifier
static NSString *simpleTableIdentifier = @"Animal3Cell";
//creates the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text =arrayItems[indexPath.row];
return cell;
}