2012-10-09 35 views
0

這是我現在的代碼,這是錯誤的。由於視圖的改變,我沒有得到viewDidLoad。如何將代表和數據源設置爲UITableViewController的視圖?

UITableView *tableView = [[UITableView alloc] init]; 
tableViewController = [[TableViewController alloc] init]; 
tableViewController.view = tableView; 
tableViewController.title = @"test"; 

tableView.delegate = self; 
tableView.dataSource = self; 

我真正需要的是改變tableViewController的觀點的delegatedatasourceself
問題是我出於某種原因不能這樣做。

類似的東西將是巨大的:

tableViewController = [[TableViewController alloc] init]; 
tableViewController.title = @"test"; 

tableViewController.view.delegate = self; 
tableViewController.view.dataSource = self; 

但由於某些原因,我不能設置tableViewController的看法財產的委託,也沒有數據源。

任何想法爲什麼?

感謝

+0

TableViewController是一個UITableViewController嗎? view是UIViewController的一個屬性,它返回一個UIView,它沒有委託或數據源。但是,UITableViewController具有tableView的tableView屬性。這些對象是相同的東西,但調用視圖會將其轉換爲UIView。只需使用.tableView。此外,在這種情況下,您不想創建和分配UITableView。如果TableViewController只是像Brandon的答案那樣的UIViewController子類,那麼您將需要創建自己的tableview。 – Matt

回答

1

確保類,你正在嘗試使用的數據源列出適當的協議和委託的方法來實現。

@interface MyNewViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

@end 

然後你應該可以做類似的事情。

tableViewController.tableView.delegate = self; 
tableViewController.tableView.datasource = self; 
相關問題