Swift
Swift

Reputation: 1184

unable to dequeue a cell with identifier ServiceDetailsTableViewCell1 in swift

i am using two tableviews in one viewcontroller, and i have given both identifiers for tableview cells in storyboard

for both cells i have added identifier in storyboard, where i am wrong pls help me

and i wrote code for both tableviewslike below

 class ServiceDetailsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var detailsTableviewheight: NSLayoutConstraint!
@IBOutlet weak var detailsTableView: UITableView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var heightOfTableView: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
    detailsTableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)

}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    let tableview = object as? UITableView
    if tableview == self.tableView{
        if(keyPath == "contentSize"){
            if let newvalue = change?[.newKey]
            {
                let newsize  = newvalue as! CGSize
                heightOfTableView.constant = newsize.height
            }
        }
    }
    
    if tableview == self.detailsTableView{
        if(keyPath == "contentSize"){
            if let newvalue = change?[.newKey]
            {
                let newsize  = newvalue as! CGSize
                detailsTableviewheight.constant = newsize.height
            }
        }
    }
    
}

deinit {
    if tableView.observationInfo != nil { tableView.removeObserver(self, forKeyPath: "contentSize")
    }
    if detailsTableView.observationInfo != nil { tableView.removeObserver(self, forKeyPath: "contentSize")
    }
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    if tableView == tableView{
        return 10
    }
    if tableView == detailsTableView{
        return 5

    }
    return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
    if tableView == detailsTableView{
        let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceDetailsTableViewCell1", for: indexPath) as! ServiceDetailsTableViewCell1
        return cell
    }
    
    if tableView == tableView{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceDetailsTableViewCell", for: indexPath) as! ServiceDetailsTableViewCell
    return cell
    }
    
    return UITableViewCell()
}
}

error:

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

Upvotes: 0

Views: 50

Answers (1)

πter
πter

Reputation: 2217

You first need to register your custom UITableviewCell's, before dequeueing it:

tableView.register(ServiceDetailsTableViewCell1.self, forCellReuseIdentifier: "ServiceDetailsTableViewCell1")
tableView.register(ServiceDetailsTableViewCell.self, forCellReuseIdentifier: "ServiceDetailsTableViewCell")

Upvotes: 0

Related Questions