iOS - 为什么表格视图不显示?

时间:2016-04-29 06:50:51

标签: ios objective-c iphone uitableview

我是iOS编程的新手,我正在学习使用storyboard来设计用户界面。

我还没有编写任何代码,因为现在所做的所有事情都在故事板中。

该程序是Tabbed Application,我用UITableView替换了第一个视图,并将其伪装成初始视图控制器。

整个结构如下:

enter image description here

但是当我在模拟器中运行它时,表格视图显示没有任何内容(我有几个表视图单元格,正如您在第一张图片中看到的那样)。

enter image description here

我无法弄清楚出了什么问题?

3 个答案:

答案 0 :(得分:1)

除非你写一些代码,否则很难看到那些单元格

首先,将一个插座从tableView拖到相应的viewController,并将其命名为tableView。

为此,请在故事板中选择自定义TableViewClass,这样您就可以将插座拖到该类文件中 enter image description here

然后:

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {...}

通过实施这两个协议来符合这两个协议

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

也不要忘记添加

self.tableView.dataSource = self;
self.tableView.delegate = self;

在你的

里面
  

viewDidLoad中()   方法

答案 1 :(得分:1)

在故事板中选择tableview,像这种委托方法一样添加。

选项A:

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

//.M file
    - (void)viewDidLoad
    {
    yourtablename.delegate = self;
    yourtablename.datasource = self;
    }

选项B:

enter image description here

答案 2 :(得分:0)

请在ViewDidLoad中添加数据源并委托给自己,或者通过适合您的故事板

- (void)viewDidLoad
{
yourtablename.delegate = self;
yourtablename.datasource = self;
}