我有一个 tableview 设置来显示来自 sqlite 数据库的数据,现在我想做的是设置一个 View ,以在用户单击表格单元格时显示来自所述数据库的更多信息。
根据我目前所读的内容,我的理解是这主要是在 TableView Controller 内的 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 中导入您的山类。
#import "MountainObject.h"
将属性更改为
@property(nonatomic, strong) MountainObject *selectedMountain
现在在之前的 View Controller 中。
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];
}
关于ios - 从 TableView 到标签的数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28831529/