0
votes

I am trying to set the Right Detail of a cell in a UITableView programmatically using Swift 3.

However, I'm unable to call the detailTextLabel.text to set it.


Currently, I have this screen. As seen, I need to set Signed in as: programmatically using a UserDefaults value.

Image 1


I've also set the Identifier of the cell:

Image 2

This is what I have tried:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "user_Cell", for: indexPath)

    let prefs:UserDefaults = UserDefaults.standard
    let user = prefs.value(forKey: "USER") as? String

    cell.detailTextLabel?.text = user

    return cell
}

However, it returns an error:

*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:6600
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier user_Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

1
Define "totally wrong".rmaddy
@rmaddy I've updated my question with the error I receivedPanda
Have you searched on that error? It's been asked and answered many times before.rmaddy
@rmaddy The problem is that it's a static table and he's trying to treat it here as a dynamic table. There are no prototype cells; the table is static.matt
Added "static" to your title just to help others find this in the future.matt

1 Answers

4
votes

This is a static table. Modifying a cell works differently for a static table. You have to implement cellForRowAt: and call super and return the resulting cell. If this is the one special cell you want to modify, modify it (using the indexPath as a test). Like this:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAt: indexPath)
    if indexPath == IndexPath(row: 0, section: 0) { // double-check this
        let prefs = UserDefaults.standard
        if let user = prefs.value(forKey: "USER") as? String {
            cell.detailTextLabel?.text = user
        }
    }
    return cell
}