以编程方式在View Controller中设置TableView

时间:2013-02-18 17:47:31

标签: ios uitableview ios5 uiview uiviewcontroller

我在视图控制器中设置了一个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");

以上方法在视图控制器中但未调用?

2 个答案:

答案 0 :(得分: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:

如果您计划支持表格编辑,则必须实施和覆盖其他方法。

答案 1 :(得分:0)

您需要设置委托&amp;表视图的数据源。

fieldView.delegate = controller; // self, if you are creating this table inside your controller
相关问题