滚动时导航栏标题发生变化

Navigation Bar Title changes when scrolling

您好,我已经在网上搜索了很长时间来解决这个问题,但我的 UITableViewController 中似乎有一个错误,当我滚动时,导航栏标题会根据我滚动的位置而改变。

Screenshot1

Screenshot2

更新: 我包含了我的 table 视图控制器代码,因为我不确定哪里出错了。我不会直接修改此代码中的导航标题,因为我可以直接在情节提要中进行修改。

似乎当我 运行 代码时,正确的标题短暂出现,一旦数据加载,标题开始根据数据奇怪地变化。

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

            }      
        }
    }

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

您要在此函数中更改页面标题 这行代码

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

更改页面标题 我为你重写了这个函数:

 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
}