我想为所选的每个单元格推送一个视图控制器,以便在按下时显示每个单元格的不同文本内容
表A.h
#import <UIKit/UIKit.h>
@interface table_A : UITableViewController
{
NSMutableArray *cars;
}
@end
我不知道我用于didSelectRowAtIndexPath的方法是否正确。
表A.m
#import "table A.h"
#import "fordviewcontroller.h"
@implementation table_A
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
cars=[[NSArray alloc]initWithObjects: @"ford",@"grand AM",@"nissan",nil];
self.title = @"cars";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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 [cars count];
}
- (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];
}
cell.textLabel.text=[cars objectAtIndex:indexPath.row];
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.
fordviewcontroller *detailViewController = [[fordviewcontroller alloc] initWithNibName:@"fordviewcontroller" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
@end
答案 0 :(得分:1)
您应该向代表汽车的fordviewcontroller
添加一个属性。所以在这种情况下你会有:
@interface fordviewcontroller : UIViewController
@property (copy) NSString *car;
@end
然后在你的表视图控制器中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = indexPath.row;
NSString *car = [cars objectAtIndex:row];
fordviewcontroller *detailViewController = [[fordviewcontroller alloc] initWithNibName:@"fordviewcontroller" bundle:nil];
detailViewController.car = car;
[self.navigationController pushViewController:detailViewController animated:YES];
}