【问题标题】:Navigation Bar Title changes when scrolling滚动时导航栏标题更改
【发布时间】:2019-01-10 18:58:45
【问题描述】:

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

Screenshot1

Screenshot2

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

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

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

            }      
        }
    }

}

【问题讨论】:

  • 您可能将您的标题设置为其他内容,可能是在 cellForRowAt 或 scrollViewDidScroll 之类的方法中。分享您的代码,以便我们更好地了解发生了什么
  • “Ergonomic”或“Synergistic”字样是否出现在数据或代码中?说到代码,那是什么样的?
  • @danh 我更新了帖子以包含我的表格视图控制器代码。 Ergonomic 和 Synergistic 这两个词确实出现了,但作为检索的数据。
  • @emrepun 我已经分享了代码,谢谢。
  • 是的,正如我所怀疑的,你在 cellForRowAt 方法中设置了你的标题,检查下面的答案。

标签: swift uitableview uinavigationbar


【解决方案1】:
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
}

【讨论】:

  • 为了更完整地解释“为什么”,UITableViewController 继承自 UIViewController。后者有一个title 属性,OP 代码无意中(通过不将其声明为局部变量)设置了该属性。 UIViewController 行为——当它是 UINavigationController 的子 vc 时,它的 title 设置器设置导航控制器的“标题”,进而在其标题栏中设置标题标签文本。正如这个答案正确指出的那样,解决方法是声明一个局部变量来保存数据的标题值。
猜你喜欢
  • 2015-12-24
  • 2022-01-12
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多