Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors with Static Table Views

Trying to work out a couple of scenes in my app my I cant get the tableViews to work as they should.

Im doing a settings like tableView where the user choses between two possible settings. Each options leads to new tableView where the user must pick one of the available options. Once the user has chosen one, the label in the first scene will display the value chosen (which is stored also as NSUserDefaults). So if Im not mistaken this could be achieved with 3 static tableViews. The code following is how Im trying to do it:

Initial settings view controller:

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 settings 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 all datasources and delegates are hooked right and UIKit is imported even if not shown.

Erros Im getting:

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier office_cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard


This is the method where I try to populate the 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 connections inspector for the UIViewController where the table is:

Outlets

And this one for the UIViewCellController:

Outlets2

Errors to this point:

UIView tableView:numberOfRowsInSection: unrecognized selector sent to instance

Terminating app due to uncaught exception NSInvalidArgumentException, reason: [UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

like image 825
Pablo Duque Avatar asked Dec 10 '15 14:12

Pablo Duque


2 Answers

If you're truly making static table view cells then you should just delete these methods. You only use them when you are dequeueing cells dynamically.

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 suggests, you need to make sure your identifier matches what you have in 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

}
like image 98
Dan Beaulieu Avatar answered Sep 28 '22 00:09

Dan Beaulieu


With the code you have provided it looks like you have not registered your table view cells and identifiers with the table view.

UITableView Class Reference

Discussion Prior to dequeueing any cells, call this method or the registerNib:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may 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 are using a nib then use

registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)
like image 23
Peter Hornsby Avatar answered Sep 28 '22 01:09

Peter Hornsby