简体   繁体   English

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

[英]iOS - Why table view doesn't show?

I'm new to iOS programming and I'm learning to use storyboard to design the UI. 我是iOS编程的新手,正在学习使用情节提要来设计UI。

I haven't wrote any code yet because all things done by now is in the storyboard. 我还没有编写任何代码,因为到目前为止完成的所有操作都在情节提要中。

The program is a Tabbed Application and I replaced the first view by a UITableView and segued it to the initial view controller. 该程序是一个Tabbed Application ,我用UITableView替换了第一个视图,并将其设置为初始视图控制器。

The whole structure is like: 整个结构如下:

在此处输入图片说明

But when I run it in the simulator, the table view showed without any content( I have several table view cells, as you can see in the first picture ). 但是当我在模拟器中运行它时,表视图显示为没有任何内容( 我有几个表视图单元格,如您在第一张图片中看到的 )。

在此处输入图片说明

I can't figured out what's wrong? 我不知道怎么了?

Unless you write some code, it would be hard to see those cells 除非您编写一些代码,否则很难看到这些单元格

First of all, drag an outlet from tableView to respective viewController and name it tableView. 首先,将出口从tableView拖到相应的viewController并将其命名为tableView。

To do that, select your custom TableViewClass in your storyboard, this will allow you to drag your outlet to that class file 为此,请在情节提要中选择自定义TableViewClass,这将使您可以将插座拖到该类文件中 在此处输入图片说明

then: 然后:

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

conform to these two protocols by implementing both 通过同时实现这两个协议

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

and

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

Also don't forget to add 也不要忘记添加

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

inside your 在你里面

ViewDidLoad() method ViewDidLoad()方法

Select tableview in storyboard and Add like this delegate method. 在情节提要中选择tableview,然后像这样的委托方法添加。

Option A: 选项A:

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

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

Option B: 选项B:

在此处输入图片说明

please add datasource and delegate to self in ViewDidLoad or through storyboard it works for you 请在ViewDidLoad或通过故事板添加数据源并将其委托给self

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM