ios - 从 TableView 到标签的数组数据

标签 ios sqlite nsmutablearray tableview

我有一个 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/

相关文章:

java - SQLiteException 没有这样的表,我错过了什么吗?

python - sqlite3 from python2.5、pysqlite和apsw有什么区别

sqlite - 如何将不同的字段值转换为SQLite中的列?

ios - 删除数组和 UITableView 中的最后一项

ios - 无法从 NSDictionary iOS 获取所有匹配字符串

objective-c - iOS 5 - 有没有办法在 UITextView 中禁用自动填充但保留拼写检查(红色下划线)?

ios - 尝试在 iOS 的 NSDateFormatter 对象中使用星期几的缩写

iOS 如何为来自 CollectionView 的图像上的 UIPreviewActions 确认 'Delete'

iOS swift : Custom scroll animation for UIScrollView

objective-c - 初始化 NSMutableArray 时使用短语法是否正确?