2

While running the code in XCode 6.1 nothing appears in the ViewController.

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    var messages: [String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(self.tableView)

        //http://stackoverflow.com/questions/25413239/custom-uitablecellview-programmatically-using-swift
        //Auto-set the UITableViewCells height (requires iOS8)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44

        // add something to messages
        messages.append("foo")
        messages.append("bar")
    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var count = messages.count ?? 0
        NSLog("message count: \(count)")
        return count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //var cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as MyCell
        var cell = UITableViewCell()
        NSLog("row[\(indexPath.row)]: \(messages[indexPath.row])")
        cell.textLabel.text = messages[indexPath.row]
        return cell
    }

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

2 Answers 2

5

Try this Code:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView = UITableView()
    tableView.dataSource = self
    tableView.delegate = self
    self.view.addSubview(self.tableView)

    //Auto-set the UITableViewCells height (requires iOS8)
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44
    tableView.frame = CGRectMake(0, 0, 320, 568)

    // add something to messages
    messages.append("foo")
    messages.append("bar")
}

EDIT:Try this for all size of screen :

tableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
Sign up to request clarification or add additional context in comments.

Comments

0

Hmm why make it so hard for your self. In the story board you can either add a TableViewController to the project or add a tableview to you viewcontroller and it will work out of the box. The controller class should inherit from UITableViewController.

2 Comments

The storyboard hides too much from you. I like being able to control my view hierarchy programmatically.
What exactly does it hide you need in the App?

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.