UIViewController中的UITableView

我如何使用uiviewcontroller里面的uitableview ? 下面是我想要做的一个例子(除了这是我的故事板中的UITableview ):

UIViewController中的UITableView

我已经想通了,我需要将委托和数据源添加到我的标题:

 //MyViewController.h @interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

在我的实现文件中,我添加了所需的方法:

 //MyViewController.m - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FileCell"]; NSLog(@"cellForRowAtIndexPath"); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]; cell.textLabel.text = [NSString stringWithFormat:@"%@",[fileListAct objectAtIndex:indexPath.row]]; return cell; } 

delegatedatasourceUITableView都连接在我的故事板中:

在Interface Builder Connections Outlet中的UITableView委托和数据源

我不能让TableView加载我告诉它的内容。 它总是空白。 为什么TableView不能填充我在cellForRowAtIndexPath方法中告诉它的内容? 我在这里错过了什么?

您必须将数据源和代理sockets从storyboard中的tableview链接到视图控制器。 这不是可选的。 这就是为什么你的表是空白的,它永远不会调用你的视图控制器的表视图方法。 (你可以通过在它们上设置断点来certificate这一点,并看到它们永远不会被触发。)你得到了什么样的构build错误?

您没有返回任何有效的值。 看到return; 没有任何数值返回?

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return; // it should be return 15; or return self.datasource.count; NSLog(@"numberOfRowsInSection"); } 

您需要为YourTableView使用IBOutlet,然后将TableView连接到YourTableView