UIViewController中的UITableView

Sam*_*cer 9 objective-c uitableview uiviewcontroller ios

如何在使用?下面是我正在尝试做的一个例子(除了这只是我的故事板中):UITableview

UIViewController中的UITableView

我已经想出我需要将委托和数据源添加到我的标头:

//MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Run Code Online (Sandbox Code Playgroud)

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

//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;
}
Run Code Online (Sandbox Code Playgroud)

delegate,datasourceUITableView都在我的故事板迷上了:

Interface Builder Connections Outlet中的UITableView Delegate和DataSource

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

jsd*_*jsd 11

您必须将故事板中tableview的dataSource和委托出口链接到视图控制器.这不是可选的.这就是为什么你的表是空白的,它永远不会调用你的视图控制器的表视图方法.(你可以通过在它们上设置断点并看到它们永远不会被触发来证明这一点.)你得到了什么样的构建错误?

  • 如果没有调用这些方法,那是因为委托/ dataSource连接不正确. (3认同)