1

Hi I've searched the web quite a while for this issue now, but it seems like theres a bug in my UITableViewController where when I scroll, the navigation bar title changes depending on where I scroll.

Pic 1 enter image description here

Pic 2 enter image description here

UPDATE: I included my table view controller code because I'm not sure where it could've gone wrong. I don't directly modify the navigation title in this code as I can do it directly in the storyboard.

It seems like when I run the code, the correct title appears briefly and once the data loads, the title begins to change weirdly according to the data.

class CustomCollectionsTableViewController: UITableViewController {
    
    // Mark: Variables
    var collections = [Collection]()
    var currentCollectionID:String!
    
    // Mark: IBOutlet
    @IBOutlet var collectionsTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Mark: Delegate
        collectionsTableView.delegate = self;
        
        // Mark: Datasource
        collectionsTableView.dataSource = self;
        
        let url = "www.url.com"
        
        ClientService.getCollections(url: url) { (receivedCollections) in
            
            self.collections = receivedCollections
            self.collectionsTableView?.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.collections.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "collectionTableViewCell", for: indexPath) as! CustomCollectionTableViewCell
        
        title = self.collections[indexPath.row].name
        
        cell.collectionTitle.text = title
        

        return cell
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "displayProducts" {
            
            if let indexPath = collectionsTableView.indexPathForSelectedRow{
                var destinationVC = segue.destination as! CollectionViewController
                let id = collections[indexPath.row].id
                currentCollectionID = String(id)
                destinationVC.collectionID = currentCollectionID

            }      
        }
    }

}
6
  • 2
    You probably set your title to something else, probably in cellForRowAt, or scrollViewDidScroll kind of methods. Share your code, so we can understand whats going on better Commented Jan 10, 2019 at 19:00
  • Do the words "Ergonomic" or "Synergistic" appear in the data or in code? Speaking of code, what's that look like? Commented Jan 10, 2019 at 19:03
  • @danh I've updated the post to include my table view controller code. The words Ergonomic and Synergistic do appear but as data that is retrieved. Commented Jan 10, 2019 at 19:10
  • @emrepun I've shared the code, thank you. Commented Jan 10, 2019 at 19:11
  • 1
    Yeah as I suspected, you set your title in cellForRowAt method, check the answer below. Commented Jan 10, 2019 at 19:28

1 Answer 1

4
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "collectionTableViewCell", for: indexPath) as! CustomCollectionTableViewCell

    title = self.collections[indexPath.row].name

    cell.collectionTitle.text = title


    return cell
}

you are changing the page title in this function this line of code

 title = self.collections[indexPath.row].name

changes the page title I rewrite the function for you to this:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "collectionTableViewCell", for: indexPath) as! CustomCollectionTableViewCell

    let temp = self.collections[indexPath.row].name

    cell.collectionTitle.text = temp


    return cell
}
Sign up to request clarification or add additional context in comments.

1 Comment

To be a little more complete on "why", UITableViewController inherits from UIViewController. The latter has a title property which the OP code inadvertently (by not declaring it as a local variable) sets. The UIViewController behavior -- when it's a child vc of a UINavigationController is to have its title setter set the 'title` of the navigation controller, which in turn sets the title label text in its title bar. The fix, as this answer points out correctly, is to declare a local variable to hold your data's title value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.