2

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.

2
  • Have you set the cell identifier to cell from the interface builder too? Commented Oct 13, 2014 at 15:48
  • @Isuru yes i have tried with that cell identifier also Commented Oct 13, 2014 at 15:58

5 Answers 5

2

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
    }

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

1 Comment

No need of declaring delegate protocols again
1

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

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

4 Comments

even i am not getting anything on my simulator ;(
Set breakpoints in your UITableViewDataSource methods to see whether they are called.
Check whether delegate and dataSource are set properly. Make sure table is not nil.
i have checked but i cant find the problem.
0

You are missing numberOfSectionsInTableView from the UITableViewDataSource protocol:

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

1 Comment

No use,have tried with the above func,even its not showing :(
0

Your 'table' delegate is not set. Add:

table.delegate = self

to your viewDidLoad above

table.dataSource = self

1 Comment

i have given but still same problem
0

you missed delegate and dataSource please add following lines in viewDidLoad

table.delegate = self

and

table.dataSource = self

2 Comments

Np, without calling them also it has worked,thank you
That's may be due to connection in storyboard/nibs.... In this case you need not to do this

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.