2013-02-18 65 views
0

我安裝我的視圖控制器內的UITableView控制器如下:設置視圖控制器內的UITableView編程方式

fieldView = [[UITableView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, (screen.size.height - logoView.bounds.size.height)) style:UITableViewStylePlain]; 
[fieldView setBackgroundColor:[UIColor greenColor]]; 
fieldView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin; 

視圖控制器是設置成爲TableViewDelegate如下:

UIViewController <UITableViewDelegate, UITableViewDataSource... 

問題:我需要做什麼來查看委託方法,以便他們控制這個添加的子視圖?

#pragma mark - 
#pragma Table View Delegate Methods 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSLog(@"Here"); 
    return 1; 
} 
- (NSString *)tableView:(UITableView *)fieldView titleForHeaderInSection:(NSInteger)section { 
    NSString *sectionName; 
    NSLog(@"Here2"); 

上面的方法是在視圖控制器,但沒有調用?

+0

'tableView:cellForRowAtIndexPath' there? – Larme 2013-02-18 17:51:20

回答

5

您需要:

// Add these right after creating the UITableView 
fieldView.delegate = self; 
fieldView.dataSource = self; 

// don't forget to add the tableview 
[self.view addSubview:fieldView]; 

您還需要所有的基本UITableViewDelegateUITableViewDataSource方法。這與您實施UITableViewController完全相同。您至少需要:

tableView:numberOfRowsInSection: 
tableView:cellForRowAtIndexPath: 

如果您計劃支持表編輯,您還需要實施和覆蓋其他方法。

+0

謝謝,我有其他方法只是錯過了委託和數據源的設置。 – StuartM 2013-02-18 18:30:14

0

您需要爲表視圖設置代表&數據源。

fieldView.delegate = controller; // self, if you are creating this table inside your controller 
相關問題