jinglei
jinglei

Reputation: 3353

iOS - Why table view doesn't show?

I'm new to iOS programming and I'm learning to use storyboard to design the 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.

The whole structure is like:

enter image description here

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).

enter image description here

I can't figured out what's wrong?

Upvotes: 2

Views: 899

Answers (3)

theDC
theDC

Reputation: 6484

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.

To do that, select your custom TableViewClass in your storyboard, this will allow you to drag your outlet to that class file enter image description here

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

Upvotes: 1

Mr. Bond
Mr. Bond

Reputation: 427

Select tableview in storyboard and Add like this delegate method.

Option A:

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

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

Option B:

enter image description here

Upvotes: 1

user5938635
user5938635

Reputation:

please add datasource and delegate to self in ViewDidLoad or through storyboard it works for you

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

Upvotes: 0

Related Questions