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

问题描述 投票:0回答:1

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

图1

图2

更新:我包含了我的表视图控制器代码,因为我不确定它可能在哪里出错。我不会直接修改这段代码中的导航标题,因为我可以直接在故事板中进行修改。

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

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

            }      
        }
    }

}
swift uitableview uinavigationbar
1个回答
3
投票
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
}
© www.soinside.com 2019 - 2024. All rights reserved.