Jeffrey
Jeffrey

Reputation: 1271

Programmatically-Created Custom UITableViewCell does not populate with given data - Swift

This hasn't gone as expected. I'm positive that I have accessed the values correctly, so why would this not populate the two cells? I would assume it'd be something simple I just passed over, but I can't figure it out for the love of me. Thanks in advance.

ViewController.swift

import UIKit

class ViewController: UITableViewController, UITableViewDelegate {

    var events: Dictionary<String, [String]> = ["0": ["Monroe Family", "La Cañada", "8:30"], "1": ["Steven Family", "Glendale", "7:30"]]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerClass(EventCell.self, forCellReuseIdentifier: "EventCell")

        tableView.dataSource = self;
        tableView.delegate = self;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension;
    }

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

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

        var cellIdendifier: String = "EventCell"

        var cell: EventCell = tableView.dequeueReusableCellWithIdentifier(cellIdendifier, forIndexPath: indexPath) as EventCell

        if let i = events[String(indexPath.row)] {
            cell.eventName.text = i[0]
            cell.eventCity.text = i[1]
            cell.eventTime.text = i[2]
        }

        cell.sizeToFit()

        return cell


    }
}

EventCell.swift

import UIKit

class EventCell: UITableViewCell {

    var eventName: UILabel = UILabel()
    var eventCity: UILabel = UILabel()
    var eventTime: UILabel = UILabel()

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

        self.contentView.addSubview(eventName)
        self.contentView.addSubview(eventCity)
        self.contentView.addSubview(eventTime)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        eventName = UILabel(frame: CGRectMake(20, 10, self.bounds.size.width - 40, 25))
        eventCity = UILabel(frame: CGRectMake(0, 0, 0, 0))
        eventTime = UILabel(frame: CGRectMake(0, 0, 0, 0))

    }

}

Upvotes: 0

Views: 1357

Answers (1)

Midhun MP
Midhun MP

Reputation: 107231

Issue is with your EventCell class.

You are adding labels to contentView in the init and you are again allocating it in the layoutSubviews. So you added a label when cell is initialized and later you re-allocated it. So you lost the connection between the first label object. So when you access those labels from the cellForRowAtIndexPath , you are getting the second label instance (that was not added to the cell) not the displaying one (first label instance)

Upvotes: 1

Related Questions