Swift: IBoutlets zero in a custom cell

I cannot understand why this user cell is not displayed.

I have a custom cell configured in a storyboard (no nib). I have a text box and 2 shortcuts that are zero when I try to access them in a custom cell class. I'm pretty sure everything is connected correctly, but still getting zero.

I selected the table cell in the storyboard and set the Custom Class to TimesheetTableViewCell I also controlled the click on the table and set the data source and delegate as TimesheetViewController

My custom cell class:

 import UIKit class TimesheetTableViewCell: UITableViewCell { @IBOutlet var duration: UITextField! @IBOutlet var taskName: UILabel! @IBOutlet var taskNotes: UILabel! required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init?(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) println("Cell initialised")// I see this println(reuseIdentifier)// prints TimesheetCell } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func setCell(duration: String, taskName: String, taskNotes: String){ println("setCell called") self.duration?.text = duration self.taskName?.text = taskName self.taskNotes?.text = taskNotes } 

My controller:

 class TimesheetViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ @IBOutlet var timesheetTable: UITableView! var items = ["Item 1", "Item2", "Item3", "Item4"] override func viewDidLoad() { super.viewDidLoad() self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell") } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("TimesheetCell", forIndexPath: indexPath) as TimesheetTableViewCell println(items[indexPath.row]) // prints corresponding item println(cell.duration?.text) // prints nil cell.setCell(items[indexPath.row], taskName: items[indexPath.row], taskNotes: items[indexPath.row]) return cell } 
+5
source share
2 answers

The problem is this line:

 self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell") 

Remove it. This line says: "Do not get the camera from the storyboard." But you want to get a cell from the storyboard.

(Make sure the cell ID is "TimesheetCell" in the storyboard, or you will crash.)

+31
source

Pretty sure you need to delete the line

 self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell") 

from viewDidLoad ().

+1
source

Source: https://habr.com/ru/post/1204856/


All Articles