【发布时间】: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 answer 和this 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