Errors with static tables

Trying to work out a couple of scenes in my application, I cannot get tableViews to work the way they should.

I make settings such as tableView , where the user chooses between two possible settings . Each parameter leads to a new tableView, where the user must select one of the available parameters . After the user selects one, the label in the first scene displays the selected value (which is also saved as NSUserDefaults). Therefore, if Im not mistaken, this can be achieved using 3 static table views. The following code: how am I trying to do this:

Controller for viewing the initial settings:

class SettingsVC2: UITableViewController{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 2 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.section == 0{ let cell = tableView.dequeueReusableCellWithIdentifier("office_cell", forIndexPath: indexPath) cell.textLabel?.text = "Select your office" return cell } else { let cell = tableView.dequeueReusableCellWithIdentifier("dept_cell", forIndexPath: indexPath) cell.textLabel?.text = "Select your department" return cell } 

One of the parameters for tableViews:

 class SettingsDepartmentTVC: UITableViewController{ let departamentos: [String] = ["Sales", "Pre-Sales", "General Administration", "Customer Service", "Profesional Services", "Regionales"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return departamentos.count } override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath) cell.textLabel?.text = self.departamentos[indexPath.row] return cell } } 

I believe that all data sources and delegates are connected correctly, and UIKit is imported even if it is not displayed.

Erros Im receipt:

Confirmation error in - [UITableView dequeueReusableCellWithIdentifier: forIndexPath:]

Application termination due to the uncaught exception "NSInternalInconsistencyException", reason: "it is impossible to disconnect the cell with the identifier office_cell - must register a thread or class for the identifier or connect the cell prototype in the storyboard


This is the method in which I try to populate a tableView:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell = tableView.dequeueReusableCellWithIdentifier("CommentsRowTVC", forIndexPath: indexPath) as! CommentsRowTVC let single_comment = self.AOS[indexPath.row] cell.titulo_comentario?.text = single_comment.titulo_comment cell.cuerpo_comentario?.text = single_comment.cuerpo_comment cell.fecha_comentario?.text = single_comment.fecha_comment return cell } class CommentsRowTVC: UITableViewCell { @IBOutlet weak var titulo_comentario: UILabel! @IBOutlet weak var cuerpo_comentario: UILabel! @IBOutlet weak var fecha_comentario: UILabel! 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 } 

}

Here is the connection inspector for the UIViewController where the table is located:

Outlets

And this one for UIViewCellController:

Outlets2

Errors to this point:

UIView tableView: numberOfRowsInSection: unrecognized selector sent to instance

The application terminated due to an unmapped NSInvalidArgumentException exception, reason: [UIView tableView: numberOfRowsInSection:]: unrecognized selector sent to the instance

+6
source share
2 answers

If you really create static table cells, you should simply remove these methods. You only use them if you dynamically change cells.

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return departamentos.count } override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath) cell.textLabel?.text = self.departamentos[indexPath.row] return cell } 

If you are actually trying to make dynamic cells , then, as your error message indicates, you need to make sure that your identifier matches what you have in the interface builder:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("{your_identifier}", forIndexPath: indexPath) cell.textLabel?.text = self.departamentos[indexPath.row] return cell } 
+2
source

With the code you provided, it looks like you have not registered the cells and table identifiers as a table.

UITableView Class Reference

Discussion Before deactivating any cells, call this method or registerNib: forCellReuseIdentifier: method to show the table view as for creating new cells. If a cell of the specified type is not currently in the reuse queue, the table view uses the provided information to automatically create a new cell object.

If you previously registered a class or nib file with the same reuse of the identifier, the class you specified in the cellClass parameter is replaced with the old record. You can specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

I would suggest that you register them in viewDidLoad() .

  let KDepartmentCellIdentifier = "department_cell" tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kKDepartmentCellIdentifier) let KOfficeCellIdentifier = "office_cell" tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kOfficeCellIdentifier) 

If you use a tip, use

 registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String) 
+1
source

All Articles