jeudi 26 février 2015

UITableViewCell Subclass in swift not appearing in TableView

Alright so I've created a subclass of UITableViewCell called TwoColumnCell, and all I want is a cell with two UILabels in it.


Here is my subclass:



import UIKit

class TwoColumnCell: UITableViewCell
{
var firstNameLabel:UILabel = UILabel()
var lastNameLabel:UILabel = UILabel()

override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)

self.contentView.addSubview(firstNameLabel)
self.contentView.addSubview(lastNameLabel)
}

override func layoutSubviews()
{
super.layoutSubviews()

firstNameLabel = UILabel(frame: CGRectMake(20.0, 10, self.bounds.size.width/2, self.bounds.size.height))
lastNameLabel = UILabel(frame: CGRectMake(self.bounds.size.width/2 + 5, 10, self.bounds.size.width/2, self.bounds.size.height))
}

override init() { super.init(); }

required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

override init(frame: CGRect) { super.init(frame: frame) }


}


Here is my Controller: import UIKit



class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate
{

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

self.tableView.registerClass(TwoColumnCell.self, forCellReuseIdentifier: "TwoColumnCell")
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 2;
}

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

var cellIdendifier: String = "TwoColumnCell"

var cell:TwoColumnCell = tableView.dequeueReusableCellWithIdentifier(cellIdendifier, forIndexPath: indexPath) as! TwoColumnCell
cell.firstNameLabel.text = "Jony"
cell.lastNameLabel.text = "Ive"

return cell
}

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


But when I run the application, the table view is empty. It's probably a CGRect issue, but I can't seem to figure it out. Any ideas?




Aucun commentaire:

Enregistrer un commentaire