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
}
}
You forgot to register your controller as a dataSource for UITableView.
Add this to your code:
@IBOutlet var myTableView: UITableView! {
didSet {
myTableView.dataSource = self
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With