[英]Load UIImageView with TableView to another ViewController
[英]Tableview to another viewcontroller
我有一個名為“firstViewcontroller”的控制器,其中我有一個名為“discoveredInstanceTableView”的UITableView
。 我想將該UITableView
加載到另一個名為“secondViewcontroller”的UIViewController
我使用了下面的代碼,但它不起作用,它說找不到屬性“discoveredInstanceTableView”......任何人請幫助我:
在第一個視圖控制器中:
IBOutlet UITableView *discoveredInstanceTableView;
在第二個視圖控制器中:
firstViewcontroller *vc1 = [[firstViewcontroller alloc]initWithNibName:@"firstViewcontroller" bundle:nil];
[self addChildViewController:vc1];
[self.myTableview addSubview:vc1.discoveredInstanceTableView];
僅當您想知道為什么上述內容不起作用時,您所問的內容才有效答案是
您正在做一些不允許的事情,根據文檔,這是無法完成的。 但是,如果我們忘記了正確的錯誤方法,您可能會添加一個表視圖作為表視圖本身的子視圖,並且我確定您將表視圖傳遞給可能未分配的表視圖。
首先想想UITableView是怎么工作的? 它只是一個在其單元格上顯示內容的 ScrollableView。
最終會建議您閱讀有關TableView 的信息
編輯:從上面的評論
重要提示:您不應在 UIScrollView 對象中嵌入 UIWebView 或 UITableView 對象。 如果這樣做,可能會導致意外行為,因為兩個對象的觸摸事件可能會混淆並錯誤處理。» 由於 UITableView 是一個 UIScrollView,這也適用於這里。
在 SecondViewController 中顯示 TableView 的可能替代方法
但是還有很多其他正確的方法可用,您應該跟進。
以編程方式或通過 XIB/故事板單獨創建一個 TableView
向當前 SecondViewController 添加委托和數據源(當發生有趣的事情時響應的方法,例如 Cell 將填充稱為cellForRowAtIndexPath
)
定義所有必需的數據源方法並編寫適當的代碼。
如果您需要在單元格點擊上執行某些操作,請添加特定的委托方法。
但是,如果您想重用 FirstViewController 類 TableView,只需創建一個 CustomView 並在其中添加 TableView,然后將該視圖添加到每個視圖控制器類中即可。
希望可以幫到你!!!
在 viewcontroller2 中聲明
@property (nonatomic, strong) UITableView *table;
在 viewcontroller1 中創建表
tableView=[[UITableView alloc]initWithFrame:CGRectMake(10, 10, 250, 300) style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.backgroundColor=[UIColor greenColor];
在調用 viewcontroller2 時將表傳遞給 viewcontroller2
ViewController2 *v2=[[ViewController2 alloc]init];
v2.table=tableView;
UINavigationController *navigation=[[UINavigationController alloc]initWithRootViewController:v2];
[self presentViewController:navigation animated:YES completion:nil];
在 viewcontroller2 中使用全局變量訪問表
[self.view addSubview:self.table];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.