UIViewController中的UITableView [英] UITableView within a UIViewController

查看:102
本文介绍了UIViewController中的UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I use a uitableview inside of a uiviewcontroller? Below is an example of what I'm trying to do (except this is just the UITableview in my Storyboard):

我是想通知我需要将委托和数据源添加到我的标题中:

I've figured out that I need to add the delegate and data source to my header:

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

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

In my Implementation file, I've added the required methods:

//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;
}

委托 datasource UITableView 都在我的故事板中连接:

The delegate, datasource, and UITableView are all hooked up in my Storyboard:

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

I can't get the TableView to load the content that I tell it to. It always comes up blank. Why won't the TableView fill with the content I tell it to in the cellForRowAtIndexPath method? What am I missing here?

推荐答案

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

You do have to link the dataSource and delegate outlets from the tableview in storyboard to the view controller. This is not optional. This is why your table is blank, it is never calling your view controller's table view methods. (You can prove this by setting breakpoints on them and seeing that they never get triggered.) What sort of build errors are you getting?

这篇关于UIViewController中的UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆