swiftuitableviewuinavigationbar

Navigation Bar Title changes when scrolling


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

            }      
        }
    }

}

Solution

  • 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
    }