IOS Swift - custom subclass of UITableViewCell not displaying content

I have a UITableView that I created on a UIStoryboard that has two dynamic prototypes of UITableViewCells:

Screenshot of UITableViewCells in Interface Builder

The screenshot will show you that I have the first UITableViewCell style set in Subtitle, and the second one is custom with the label “Tap to Add” in the center. The first has the identifier "Cell" and the second is "AddCell". I set up the UITableViewController (I also tried the UITableView in the UIViewController), a subclass of the UITableViewCell in Swift, and I connected all of my outputs. However, when I run the simulator, the cell loads and it is available, but I could not display its contents. (I tried to add other controls, but nothing will appear when the cell is loaded. The only thing I can change is the contentView backgroundColor.)

I have the following Swift code for a 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 an empty cell visible when selected: enter image description here

I double-checked everything related to my conclusions that the class names were set correctly in Interface Builder, I registered the class of my UITableViewCell with tableView, and everything seems to be configured correctly. Is it possible that this is a mistake? Any help would be greatly appreciated.

+7
swift
source share
2 answers

I believe this is due to calibration in the Storyboard. It seems that now he likes default to a broader view, and most people, including me (and judging by your width of viewing the storyboard, you) prefer to have the width set to "Compact".

In the storyboard, try setting your width / height for any / any or in the inspector so that your labels inside the cells scroll all the way down and play the “Installed” checkbox, and you will notice that it has various options for choosing the size of the class. If it looks like mine, you will have the first “Installed” that is not installed, and the second for your calibration parameter. I deleted the user "Installed" and checked the default value, and then moved the labels.

I do not believe that I have enough reputation to post more than 1 image or 2 links, I would like to explain what I mean.

+10
source share

I had many hours to find out why my example with the same question as here did not work. I use message boards and fast 2 (xcode 7.2).

As soon as i removed

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

in viewDidLoad (), this worked for me. I just used dequeueReusableCellWithIdentifier (), as indicated in this example, populated a cell, i.e. ...

Regards, Michelle

+4
source share

All Articles