簡體   English   中英

從表視圖到標簽的數組數據

[英]Array data from table view to label

我有一個tableview設置來顯示來自sqlite數據庫的數據,現在我要做的是設置一個視圖,以在用戶單擊表格單元格時顯示來自所述數據庫的更多信息。

從到目前為止的內容中,我的理解主要是在表視圖的控制器內部的prepareForSegue方法中完成的?

無論如何,經過大量的搜索之后,我最終從AppCoda教程(鏈接)中獲得了這一點...

 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

現在在以前的視圖控制器中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM