簡體   English   中英

一個UIViewController中的UITableView和UIView

[英]UITableView and UIView in one UIViewController

我想開發一個iPad應用程序。 我有一個包含UITableViewUIViewUIViewController ,在我的文件.xib中,我有這兩個控制器。 在我的ViewController.h :我有UITableView *tableUIView *viewContent

ViewController.m :我開發了一些函數,需要將數據加載到表視圖中。 現在,我需要將數據從UITableView傳遞到UIView 我使用didSelectRowAtIndexPath但不起作用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
     // I get the object activite : here no problem 
     IPADAG1Activity  *activite = [[objects objectForKey:[objectsIndex objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
     UIView *contentView = [[UIView alloc]init ];
     //now I instanciate my UIViewController wich contain  : uiview *contentview and uitableview
     GSAActivityViewController *activity = [[GSAActivityViewController alloc]init];
     //in uiview I add label "desc" that I affect the value of activite.DESCRIPTION
     activity.desc.text = activite.DESCRIPTION;
}
IPADAG1Activity  *activite = [[objects objectForKey:[objectsIndex objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

您是否正在激活數據?DESCRIPTION

問題在這里

 GSAActivityViewController *activity = [[GSAActivityViewController alloc]init];
 //in uiview I add label "desc" that I affect the value of activite.DESCRIPTION
activity.desc.text = activite.DESCRIPTION;

您尚未將activity.view添加到self.view中。

您需要設置tableViewdelegate

self.tableView.delegate = self;

或者,您可以在IB中這樣做:

在此處輸入圖片說明

我假設此代碼是您的GSAActivityViewController類的一部分。 話雖如此,您無需創建新的GSAActivityViewController並更改該控制器上的標簽,因為您已經初始化了控制器。

這是您的代碼在做什么:

 //This line is getting your IPADAG1Activity correctly it looks like, this is fine
 IPADAG1Activity  *activite = [[objects objectForKey:[objectsIndex objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];


 //This code is creating a brand new UIView called *contentView, but it never gets used.  I am guessing that you are getting a warning about an unused variable here. ?  This line does not need to be in the program as far as I can tell because you don't need to create a new contentView, you just need to use the on GSAActivityViewController
 UIView *contentView = [[UIView alloc]init ];


 //The line below is creating a brand new instance of GSAActivityViewController, but you don't want a brand new isntance, you want to use this GSAActivityViewController that has already been initialized.  This line does not need to be in the program as fas as I can tell.
 //now I instanciate my UIViewController wich contain  : uiview *contentview and uitableview
 GSAActivityViewController *activity = [[GSAActivityViewController alloc]init];


 //Lastly, you are changing the text of desc on the new GSAActivityViewController that you created.  This controller did not need to be created and is not being used in any way other than this function block, so when you change the text on desc you are changing the text on something that has not been displayed and is just about to get tossed out when this function ends (Assuming ARC)
 //in uiview I add label "desc" that I affect the value of activite.DESCRIPTION
 activity.desc.text = activite.DESCRIPTION;

因此,要解決您的問題,您可以使用self獲取當前的GSAActivityViewController 您不需要初始化一個新的(或一個新的contentView )。

因此,您的代碼應如下所示:

 IPADAG1Activity  *activite = [[objects objectForKey:[objectsIndex objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

 //in uiview I add label "desc" that I affect the value of activite.DESCRIPTION
 self.desc.text = activite.DESCRIPTION;

該答案基於一些假設,主要是descGSAActivityViewController的屬性的標簽,並且此代碼位於GSAActivityViewController

暫無
暫無

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

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