简体   繁体   English

通过tableView上的segue传输数据

[英]Transmit data through segue on tableView

I'm working on an app where I want to display a tableView populated with users, and when the row is selected a new view is displayed containing a conversation (so basically a messaging app). 我正在开发一个应用程序,该应用程序要显示一个填充有用户的tableView,当选择该行时,将显示一个包含对话的新视图(因此基本上是一个消息传递应用程序)。 For now I just want that when I touch a row on my FirstViewController, the SecondViewController is displayed with the name of the user selected on a label. 现在,我只希望当我在FirstViewController上触摸一行时,显示SecondViewController以及在标签上选择的用户名。 But I can't get it to work because each time I touch a row, the indexPath is 0, si it is always the first user name that is displayed. 但是我无法使用它,因为每次我触摸一行时,indexPath为0,因为它始终是显示的第一个用户名。 Here is a part of my code: 这是我的代码的一部分:

#import "GSBChatViewController.h"
#import "GSBConversationViewController.h"



@interface GSBChatViewController ()
@property (weak, nonatomic) IBOutlet UITableView *userTableView;

@end

@implementation GSBChatViewController
@synthesize chatUsers;

- (void)viewDidLoad {
[super viewDidLoad];

GSBChatUsers *user1 = [[GSBChatUsers alloc]initWithName:@" John DOE" andPicture:@"photo.jpg" andLastMessage:@"Ca marche!"];
GSBChatUsers *user2 = [[GSBChatUsers alloc]initWithName:@"Justine DUBOIS" andPicture:@"photo.jpg" andLastMessage:@"Salut, ça va?"];
GSBChatUsers *user3 = [[GSBChatUsers alloc]initWithName:@"Jacques LAPORTE" andPicture:@"photo.jpg" andLastMessage:@"Réunion le 23 à 15h, c'est bon pour toi?"];
GSBChatUsers *user4 = [[GSBChatUsers alloc]initWithName:@"Guillaume DUPOND" andPicture:@"photo.jpg" andLastMessage:@"OK, parfait"];
GSBChatUsers *user5 = [[GSBChatUsers alloc]initWithName:@"Françoise MARTIN" andPicture:@"photo.jpg" andLastMessage:@"Tu as posé tes congés?"];
GSBChatUsers *user6 = [[GSBChatUsers alloc]initWithName:@"Jean-Jacques CELESTIN" andPicture:@"photo.jpg" andLastMessage:@"Je prends note"];

chatUsers = [[NSArray alloc]initWithObjects:user1,user2,user3,user4,user5,user6, nil];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



#pragma mark - Navigation


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

NSIndexPath *indexPath = [self.userTableView indexPathForCell:sender];
NSLog(@"Path: %@",indexPath);

GSBConversationViewController *destVC = [segue destinationViewController];
GSBChatUsers *selectedUser =[chatUsers objectAtIndex:indexPath.row];

NSLog(@"%ld",[self.userTableView indexPathForCell:sender].row);
NSString *userName = selectedUser.name;

NSLog(userName);
destVC.Test=userName;

}


#pragma mark - Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Retourne le nombre d'éléments de notre liste
NSLog(@"Number of rows: %ld",chatUsers.count);
return [chatUsers count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// Instancie notre cellule par son identifier
GSBTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell"];

// On récupère l'item que l'on va traiter
GSBChatUsers *user = [chatUsers objectAtIndex:indexPath.row];

// On affecte les valeurs de notre user aux éléments de notre cellule
[cell.userName setText:user.name];
[cell.profilePicture setImage:[UIImage imageNamed:user.picture]];
[cell.lastMessage setText:user.lastMessage];
NSLog(@"%@",chatUsers);

return cell;

}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%@",_cellTitle);
//[self performSegueWithIdentifier:@"conversationSegue" sender:[chatUsers objectAtIndex:indexPath.row]];

}


@end

I looked at this answer as well as this one but none of them helped. 我看着这个答案 ,以及这一个 ,但他们没有帮助。 (Well they helped because beforehand I couldn't even get the label's text to change). (好吧,他们提供了帮助,因为事前我什至无法更改标签的文字)。

Disclaimer: This is for a school project, and I'm one hundred percent allowed to ask for help on the Internet. 免责声明:这是针对学校项目的,我被允许百分之一百在互联网上寻求帮助。 As english is not my native language maybe I'm not clear on some points, please let me know if you need further informations. 由于英语不是我的母语,也许我在某些方面不清楚。如果您需要更多信息,请告诉我。

Thanks for your help! 谢谢你的帮助!

it looks like the tableView in the storyboard is not connected to the outlet userTableView . 似乎情节userTableView的tableView未连接到出口userTableView therefore the row NSIndexPath *indexPath = [self.userTableView indexPathForCell:sender]; 因此,行NSIndexPath *indexPath = [self.userTableView indexPathForCell:sender]; does not return the correct result. 没有返回正确的结果。

go to your storyboard, connect the outlet to the tableview and try again! 转到您的情节提要,将插座连接至桌面视图,然后重试!

插座未连接

If you dragged a segue on the storyboard from your table cell prototype to the view controller you want to push to, then you are halfway there. 如果将情节提要中的序列从表单元原型拖到要推送到的视图控制器,那么您就已经完成了一半。 However, the segue will be triggered before the UITableViewDelegate is informed that the user "didSelectRowAtIndexPath", so navigation to the next view controller will happen before your code in that delegate method runs. 但是,将在通知UITableViewDelegate用户“ didSelectRowAtIndexPath”之前触发segue,因此将在运行该委托方法中的代码之前导航到下一个视图控制器。

The solution is to override prepareForSegue:sender: in your FirstViewController. 解决方案是在FirstViewController中重写prepareForSegue:sender: :。 In this case, the sender will be the table cell that was tapped. 在这种情况下,发送者将是被点击的表单元格。

Your implementation could look something like this: 您的实现可能如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue
             sender:(id)sender {
    GSBTableViewCell *cell = (GSBTableViewCell *)sender;
    SecondViewController *destination = ((SecondViewController *)segue.destinationViewController);
    destination.view;  // Sadly, this is required to force the outlets to be connected in the destination view controller before you try to access them
    destination.userNameLabel.text = cell.userName.text;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM