从 table 视图到标签的数组数据

Array data from table view to label

我有一个 table 视图设置来显示来自 sqlite 数据库的数据,现在我想做的是设置一个视图,以便在用户单击 table单元格。

根据我目前所读的内容,我的理解是这主要是在 table 视图控制器中的 prepareForSegue 方法中完成的?

无论如何,经过大量谷歌搜索后,我从 AppCoda 教程中得到了这个 (link)...

 if([segue.identifier isEqualToString:@"pushToMountainInfo"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UINavigationController *nav = segue.destinationViewController;
    MountainInformation *destViewController = [nav.viewControllers objectAtIndex:0];
    MountainsObject *mountain = [self.mountains objectAtIndex:indexPath.row];
    destViewController.nameLabel = mountain.name;
    NSLog(@"%@", mountain.name);
}

我把 NSLog 放在那里进行测试,它确实将山的名称输出到日志中,但是当我 运行 应用程序时标签仍然空白。

有人可以帮忙吗? 谢谢..!

可以稍微缩短您的代码..

 if([segue.identifier isEqualToString:@"pushToMountainInfo"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

MountainInformation *destViewController = [segue destinationViewController];// do this only if your segue is connected to the next one from previous, or else let it stay as you have, only change the later part
MountainsObject *mountain = [self.mountains objectAtIndex:indexPath.row];
destViewController.myMountain = mountain.name;
NSLog(@"%@", mountain.name);
}

现在在 MountainInformation.h 文件中创建一个 属性

 @property (nonatomic, strong) NSString* myMountain;

现在您的 属性 已设置为您要设置的名称。 现在在 MountainInformation.m

中的 viewDidLoad 中
-(viewDidLoad){
self.labelToShowName.text = self.myMountain;
}

希望对您有所帮助

[编辑] 在 MountainInformation.h 中导入您的山 class。

#import "MountainObject.h"

将 属性 更改为

@property(nonatomic, strong) MountainObject *selectedMountain

现在在以前的视图控制器中。

 if([segue.identifier isEqualToString:@"pushToMountainInfo"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *nav = segue.destinationViewController;

MountainObject *mountain = [self.mountain objectAtIndex.indexPath.row];
destViewController.selectedMountain = mountain;
}

在MountainInformation.h

-viewDidLoad{
NSString *mountainName = self.selectedMountain.name;
NSString *mountainRegion =self.selectedMountain.region;
NSString *mountainHeight =self.selectedMountain.height;
//and other properties. and then you can set it to a single label by using
self.myLabel.text = [NSString stringWithFormat:@" Name - %@, Height- %@, Region- %@",mountainName, mountainHeight, mountainRegion];
}