Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying array items in tableview with swift

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
    }

}
like image 602
t1w Avatar asked Mar 12 '15 13:03

t1w


2 Answers

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

@IBOutlet var myTableView: UITableView! {
    didSet {
        myTableView.dataSource = self
    }
}
like image 92
Nikita Kukushkin Avatar answered Oct 08 '22 05:10

Nikita Kukushkin


    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

like image 21
Ramprasath Selvam Avatar answered Oct 08 '22 06:10

Ramprasath Selvam