将单击的单元格的文本发送到下一个视图

Send the text of cell clicked, to the next view

1.1,我有一个 TableView,与 sqlite 链接,当我单击一个单元格时,程序转到 prepareForSegue,然后在 tableView didSelectedRow 中获取值。

为此,在第一次访问 secondView 时,Label 的值为 nil,如果我回到上一个视图和 select 其他单元格,我将获得第一个单元格的值。

我在 secondView 中有一个 UILabel,此文本将是单元格 selected。

我不知道如何在 segue 中说出 tableView 的文本值。

可能有人可以帮助我,如果我的英语不好,请见谅。

谢谢。

#import "farmacopeaViewController.h"

import "AddFarmacoViewController.h" //第二个视图`

@implementation farmacopeaViewController

@synthesize copiaFarmaco,farmacos,descript;

- (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // 如果没有父视图,则释放视图。 [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

- (void)viewDidLoad { [super viewDidLoad];

[self populateFarmacos]; [self populateDescrip]; }

-(void) populateFarmacos { self.farmacos = [[NSMutableArray alloc] init];

FMDBDataAccess *db = [[FMDBDataAccess alloc] init];

self.farmacos = [db getFarmacos]; } -(void) populateDescrip { self.descript = [[NSMutableArray alloc] init];

FMDBDataAccess *db = [[FMDBDataAccess alloc] init];

self.descript = [db getDescripcion]; }

- (void)viewDidUnload { [super viewDidUnload]; }

- (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 对于支持的方向 return (interfaceOrientation == UIInterfaceOrientationPortrait); }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.

return 1; }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

self.copiaFarmaco = [[[tableView cellForRowAtIndexPath:indexPath] textLabel]text]; NSLog(@"%@",copiaFarmaco); } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"pasarDatos"]){

    AddFarmacoViewController *segundoView = [segue destinationViewController];

    segundoView.nombre = self.copiaFarmaco;
}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.farmacos 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]; } Farmaco *farmaco = [farmacos objectAtIndex:[indexPath row]];

[[cell textLabel] setText:[NSString stringWithFormat:@"%@",farmaco.farmacoId]]; return cell; }

@end

您应该创建从第一个 ViewController 到第二个 ViewController 的转场(而不是从一个按钮到第二个 ViewController)

在你的didSelectRowAtIndexPath

里面
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
         [self performSegueWithIdentifier:@"segueToLocation" sender:the object you want to pass];
    }

然后在这里获取对象

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString: @"segueToLocation"]) {    
         SecondViewController *dest = (SecondViewController *)[segue destinationViewController];
         //the sender is what you pass into the previous method
         dest.labelValue=sender
    }
}