30

I have a ViewController in the MainstoryBoard. I added the TableView to it.

MainStoryBoard:

enter image description here

In addition I have an array outside of the ViewController class and I want the objects inside the array to be shown in the TableView.

I can't figure out how to do it. I connected the delegate between the TableView and the ViewController.

8
  • 1
    refer this link ralfebert.de/tutorials/ios-swift-uitableviewcontroller weheartswift.com/… Commented Jul 28, 2015 at 10:30
  • 1
    @DharmeshDhorajiya Great, It works! "weheartswift" has the answer Commented Jul 28, 2015 at 11:24
  • 1
    Welcome any time @Eliko you every this is fine on google. Commented Jul 28, 2015 at 11:27
  • 1
    @DharmeshDhorajiya do you know what I need to do if I want to put 2 table views in 1 view controller? Commented Jul 28, 2015 at 15:26
  • 1
    first you set every tableView datasource and delegate then you set tableView tag. for ex table1 tag is 101 and second table tag is 102. Then u check every tableView delegate method like this way. if(table1.tag==101){ //table1 cell coding}else { //second table cell code} Commented Jul 28, 2015 at 15:35

2 Answers 2

56

You add a new table view instance variable below the class declaration.

@IBOutlet weak var tableView: UITableView!

To conform to the UITableViewDelegate and UITableViewDataSource protocol just add them separated by commas after UIViewController in the class declaration

After that we need to implement the tableView(_:numberOfRowsInSection:), tableView(_:cellForRowAtIndexPath:) and tableView(_:didSelectRowAtIndexPath:) methods in the ViewController class and leave them empty for now

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    ...

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0 // your number of cells here
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // your cell coding 
        return UITableViewCell()
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        // cell selected code here
    }
}

As mentioned by @ErikP in the comments, you also need to set

self.tableView.delegate = self

self.tableView.dataSource = self

in the viewDidLoad method.

(or in Storyboard works well too).

Sign up to request clarification or add additional context in comments.

5 Comments

I thought this was going to work but when I get to my do_table_refresh function and call: self.tableView.reloadData() it fails with fatal error: unexpectedly found nil while unwrapping an Optional value
You will need to set self.tableView.delgate = self and self.tableView.dataSource = self in the viewDidLoad method.
@IBOutlet weak var tableView: UITableView! Write weak to except retain cycle
In your storyboard, don't forget to Drag from the table view to the yellow circle on top of the view controller. Let go and choose "dataSource". Do this again but choose delegate instead.
It's also useful to add self.tableView.dataSource = self; self.tableView.delegate = self;
1

i might be late and you may have fixed this by now. The error you are getting is due to your variable or constant returning a nil value. to test this you can assign a value to it (hard code it) and check the full code if it is working and then change it to your array, unfortunately i do stuff programmatically and not very familiar with the storyboard.

if you share your code we will be assist you if this is not yet sorted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.