Upvote:7

Aprove answer
import #UIKit

class myCell: UITableViewCell {

    // Define label, textField etc
    var aMap: UILabel!

    // Setup your objects
    func setUpCell() {
        aMap = UILabel(frame: CGRectMake(0, 0, 200, 50))
        self.contentView.addSubview(aMap)
    }
}


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView = UITableView()
    // for ex, lets say, your data array is defined in the variable below
    var dataArray = [[String:AnyObject]]() //Array of your data to be displayed

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = UIColor.whiteColor()

        // register your class with cell identifier
        self.tableView.registerClass(myCell.self as AnyClass, forCellReuseIdentifier: "Cell")

        self.view.addSubview(tableView)

        dataArray = // Something loaded from internet 
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

       // let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)

        var cell:myCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? myCell

        if cell == nil {
            cell = myCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }
        var data = dataArray[indexPath.row]
        cell?.setUpCell()
        cell!.aMap.text = String(dict["productName"])
        return cell!
    }
}

Upvote:2

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let lable1String = "lbl1"
        let lable2String = "lbl2"
        let lable3String = "lbl3"

        var cell : PackageListTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellID") as?PackageListTableViewCell

        if (cell == nil) {
            cell = PackageListTableViewCell.init(style: UITableViewCellStyle.Default,
                reuseIdentifier:"cellID");

        }

        cell.selectionStyle = UITableViewCellSelectionStyle.None;
        //set text of your lables as below
        cell.label1.text = lable1String;

        return cell;
    }

More Answer related to the Same Query

Upvote:0

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
    tableView.dataSource = self
    tableView.delegate = self
    tableView.backgroundColor = UIColor.whiteColor()

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myIdentifier")

    tableView.frame = CGRectMake(0 , 0, self.view.bounds.width, self.view.bounds.height)//Optional for table size

    self.view.addSubview(tableView)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)
    myCell.textLabel?.text = "\(indexPath.row)"
    myCell.detailTextLabel?.text = "Subtitle"

    return myCell
}

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

Credit Goes to: stackoverflow.com

Related question with same questions but different answers