【问题标题】:Transmit data through segue on tableView在 tableView 上通过 segue 传输数据
【发布时间】:2016-03-22 21:51:50
【问题描述】:

我正在使用一个应用程序,我想在其中显示一个带有用户的表视图,并且选择该行时,显示了包含对话的新视图(因此基本上是消息传递应用程序)。现在我只希望当我在我的 FirstViewController 上触摸一行时,SecondViewController 会显示标签上选择的用户名。但我无法让它工作,因为每次我触摸一行时,indexPath 为 0,si 它始终是显示的第一个用户名。这是我的代码的一部分:

#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

我查看了this answerthis one,但都没有帮助。 (好吧,他们提供了帮助,因为事先我什至无法更改标签的文本)。

免责声明:这是一个学校项目,我百分百可以在互联网上寻求帮助。 由于英语不是我的母语,也许我在某些方面不清楚,如果您需要更多信息,请告诉我。

感谢您的帮助!

【问题讨论】:

  • 所以你已经通过故事板中的拖放将segue从FirstViewController连接到SecondViewController
  • @AndréSlotta 是的,从firstViewController的原型cell到secondViewController
  • 那么在这种情况下,您不需要didSelectRowMethod,而只需要prepareForSegue。在prepareForSegue 中,它现在总是打印出 0 行?
  • @AndréSlotta 好的,我会删除它。现在 tableView 上的第一个名字是 John Doe,这是在 secondVC 的标签上显示的每一行的名字(尽管所有行都有不同的名字)。 NSLog(@"%ld",[self.userTableView indexPathForCell:sender].row); 在控制台中总是给我“0”。
  • 这真的很奇怪。你的代码看起来不错。你能把你的项目分享给我吗?

标签: ios objective-c iphone uitableview segue


【解决方案1】:

故事板中的 tableView 似乎未连接到插座 userTableView。因此行 NSIndexPath *indexPath = [self.userTableView indexPathForCell:sender]; 不会返回正确的结果。

转到您的故事板,将插座连接到 tableview 并重试!

【讨论】:

  • 感谢您抽出宝贵时间帮助我!我试过了,它仍然无法正常工作。我现在要睡觉了,所以明天我会检查所有网点的连接,因为如果我忘记了一个,我就有可能忘记其他的。顺便说一句,如果您想测试它,您不需要密码,只需触摸登录页面上的徽标!如果我发现了什么,我会通知你,再次感谢您的宝贵时间!
  • 它有效!你必须连接tableview的插座,就像我在我的解决方案中写的那样,你还必须删除segue一次,然后重新安装单元格到目标视图控制器。瞧! :)
  • 太好了!非常感谢你的帮助!我从来没有想过检查网点,我想我现在会考虑的! :)
【解决方案2】:

如果您将情节提要上的 segue 从表格单元原型拖到要推送到的视图控制器,那么您就成功了。但是,segue 将在 UITableViewDelegate 被告知用户“didSelectRowAtIndexPath”之前触发,因此导航到下一个视图控制器将在您的委托方法中的代码运行之前发生。

解决方案是在您的 FirstViewController 中覆盖 prepareForSegue:sender:。在这种情况下,发送者将是被点击的表格单元格。

您的实现可能如下所示:

- (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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多