PushViewController 从 didSelectRowAtIndexPath 的单元格中显示推送时每个单元格的不同文本内容

发布于 2024-12-26 06:08:11 字数 2900 浏览 2 评论 0原文

我想为每个选定的单元格推送一个视图控制器,以便在推送

表格时为每个单元格显示不同的文本内容啊

#import <UIKit/UIKit.h>

@interface table_A : UITableViewController
{
    NSMutableArray *cars;
}

@end

我不知道我用于 didSelectRowAtIndexPath 的方法是否正确。

表上午

#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

i want to push a view controller for each cell selected, to show different text content for each cell when pushed

table A.h

#import <UIKit/UIKit.h>

@interface table_A : UITableViewController
{
    NSMutableArray *cars;
}

@end

I don't know if the method I'm using for didSelectRowAtIndexPath is correct.

table 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2025-01-02 06:08:11

您应该向代表汽车的 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];
}

you should add a property to fordviewcontroller that represents the car. so in this case you'd have:

@interface fordviewcontroller : UIViewController

@property (copy) NSString *car;

@end

then in your table view controller:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文