Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift - UITableViewCell Custom Subclass not displaying content

Tags:

swift

I have a UITableView that I've created in a UIStoryboard that has two Dynamic Prototype UITableViewCells:

Screenshot of UITableViewCells in Interface Builder

The screenshot will show you that I have the first UITableViewCell's style set to Subtitle, and the second is set to custom with a label "Tap to Add" in the center. The first has an identifier of "Cell" and the second "AddCell". I've set up a UITableViewController (I've also tried a UITableView in a UIViewController), UITableViewCell subclass in Swift and I've connected all of my outlets. However, when I run the simulator the cell is loaded and it is tappable, but I have not been able to get it to display any content. (I've tried adding other controls, but nothing will appear when the cell is loaded. The only thing that I can change is the contentView's backgroundColor.)

I have the following Swift code for the UITableViewController:

import UIKit

class ListTableViewController: UITableViewController {

    var listObjects: ListObject[] = DataManager.instance.allListObjects() as ListObject[]

    init(style: UITableViewStyle) {
        super.init(style: style)
        // Custom initialization
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(AddListObjectTableViewCell.classForCoder(), forCellReuseIdentifier: "AddCell")
    }
    @IBAction func editButtonPressed(editButton: UIBarButtonItem) {
        if (self.tableView.editing) {
            editButton.title = "Edit"
            self.tableView.setEditing(false, animated: true)
        } else {
            editButton.title = "Done"
            self.tableView.setEditing(true, animated: true)
        }
    }

    // #pragma mark - Table view data source

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

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return listObjects.count + 1
    }


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

        let cellIdentifier = (indexPath.row < listObjects.count) ? "Cell" : "AddCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell

        if (indexPath.row < listObjects.count) {
            let currentListObject : ListObject = listObjects[indexPath.row]
            cell.textLabel.text = currentListObject.name
            cell.detailTextLabel.text = currentListObject.detail
        } else {
            cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as AddListObjectTableViewCell
            if (cell == nil) {
                cell = AddListObjectTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
            }
        }

        return cell
    }

    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        if (indexPath.row < listObjects.count) {

        } else {
            self.performSegueWithIdentifier("AddListObjectShow", sender: self)
        }

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView?, canEditRowAtIndexPath indexPath: NSIndexPath?) -> Bool {

        return (indexPath?.row < listObjects.count) ? true : false
    }}

I also have the following Swift for my UITableViewCell:

import UIKit

class AddListObjectTableViewCell: UITableViewCell {

    @IBOutlet var addLabel : UILabel

    init(style: UITableViewCellStyle, reuseIdentifier: String) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // Initialization code
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }}

Finally, here is a screenshot of the simulator with the empty cell visible when selected: enter image description here

I've double checked all that all of my outlets are connected, that my class names are set properly in Interface Builder, I've registered the class of my UITableViewCell with the tableView, and everything seems to be configured correctly. Is it possible this is a bug? Any help would be greatly appreciated.

like image 937
Aron C Avatar asked Jun 26 '14 03:06

Aron C


2 Answers

I believe it has to do with the sizing in Storyboard. It seems now it likes to default to a wider view and most people including me (and judging by your storyboard view width, you) prefer having the width set to 'Compact'.

In storyboard either try setting your width/height to any/any or in the inspector for your labels inside the cells scroll all the way down and play the 'Installed' checkbox and you'll notice it has various options for the sizing class. If it's like mine, you'll have the first 'Installed' one unchecked and a second one for your sizing option checked. I removed the custom 'Installed' and checked the default one and then moved my labels into place.

I don't believe I have enough reputation to post more than 1 image or 2 links, wish I could to explain what I mean easier.

like image 71
jmck Avatar answered Oct 02 '22 19:10

jmck


I had many hours in finding out, why my sample with the same issue as stated here wasn't working. I'm using story boards and swift 2 (xcode 7.2).

Once I removed

self.tableView.registerClass(AddListObjectTableViewCell.classForCoder(), forCellReuseIdentifier: "AddCell")"

in viewDidLoad() it worked for me. I just used dequeueReusableCellWithIdentifier() as stated in this sample, filled the cell, that was it...

Kind regards, Michel

like image 34
Michel Clément Avatar answered Oct 02 '22 18:10

Michel Clément