6

I cannot display elements of an array in a tableview by using swift programming language. I did some research but could not solve the problem. Below, you can see my code and download project as a .zip file from here: http://1drv.ms/1Ca2hyp

I can also give some videos. They're 11 min and 10 sec. long in total

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var myTableView: UITableView!
    var cars = [String]()
    var newCar: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        cars = ["BMW","Audi", "Volkswagen"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cars.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = cars[indexPath.row]

        return cell
    }

}
2
  • What happens when you run your app? Are you receiving an error message, or do you just see nothing on the screen? Did you make sure to set the cell identifier for the cell on the Storyboard or xib file to "myCell"? Commented Mar 12, 2015 at 13:54
  • @Alexander table cell on storyboard has same identifier as "myCell". When i run the code, it gives no error Commented Mar 12, 2015 at 13:55

3 Answers 3

8

You forgot to register your controller as a dataSource for UITableView.
Add this to your code:

@IBOutlet var myTableView: UITableView! {
    didSet {
        myTableView.dataSource = self
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3
    class SelectCategoryViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
        var CategeryArray = ["Food","Travel","Lifestyle","Card","MyReserve","Game","Songs","Movies","Entertainment","Business","Education","Finance","Drink","Sports","Social","Shopping"]



    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            self.tableView.dataSource = self
            self.tableView.delegate = self
        }
}
    extension SelectCategoryViewController: UITableViewDelegate,UITableViewDataSource {

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return CategeryArray.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell")!
            cell.textLabel?.text = self.CategeryArray[indexPath.row]
            return cell
        }
    }

set the identifier =TableViewCell in Main.storyboard
And also set the class name for TableViewCell

Comments

2

Maybe you forgot to add these two lines to your view controller's viewDidLoad method:

yourTableView.delegate = self

yourTableView.dataSource = self

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.