Santo
Santo

Reputation: 1651

creating table view using swift

I have been trying to create a table view using swift using xcode 6 and ios8 but I am not getting anything on my simulator,simply it showing empty(white).

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var myArr = ["one","two","three","four","five"]

    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        cell.textLabel?.text = self.myArr[indexPath.row]


        return cell;
    }

}

I have even tried by dragging some elements like button, switch, etc, and I am not getting anything on simulator. If I download and run any code its working.

Upvotes: 2

Views: 2792

Answers (5)

Santo
Santo

Reputation: 1651

Here my worked code, i dont understand what was wrong in previous one,but i have created a new project then it worked. i just dragged a table view to viewcontroller and assigned delegate and datasource then came to viewcontroller.swift and tried

import UIKit

class ViewController: UIViewController {

    var items = ["a","b","c"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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


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

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myCell") as UITableViewCell
        cell.textLabel?.text = self.items[indexPath.row]
        return cell
    }

}

Upvotes: 2

pravin salame
pravin salame

Reputation: 349

you missed delegate and dataSource please add following lines in viewDidLoad

table.delegate = self

and

table.dataSource = self

Upvotes: 0

Valentin
Valentin

Reputation: 3302

Your 'table' delegate is not set. Add:

table.delegate = self

to your viewDidLoad above

table.dataSource = self

Upvotes: 0

zisoft
zisoft

Reputation: 23078

You are missing numberOfSectionsInTableView from the UITableViewDataSource protocol:

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

Upvotes: 0

Kirsteins
Kirsteins

Reputation: 27345

Is dataSource of table set in Interface builder? You can do it in code as well:

override func viewDidLoad() {
    super.viewDidLoad()
    table.dataSource = self    
}

Upvotes: 1

Related Questions