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

标签 swift uitableview uinavigationbar

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

Screenshot1

Screenshot2

更新:我包含了我的 TableView Controller 代码,因为我不确定哪里可能出错了。我不会直接修改此代码中的导航标题,因为我可以直接在 Storyboard 中进行修改。

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

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
}

关于swift - 滚动时导航栏标题发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54135269/

相关文章:

ios - 当第一个 View Controller 在 iOS 中隐藏其导航栏时,向后滑动会导致导航栏立即隐藏(不平滑)

uitableview - ViewController 没有名为 tableview 的成员错误

ios - 如何仅引用indexPathsForVisibleRows中的第一个对象

objective-c - 类似于 Mobile Safari 的可滚动 UINavigationBar

快速便利初始化和通用类

ios - ios UI更新代码如何定义用于主调度

string - 如何实现在键入要搜索的字符串时查找一个字符串在另一个字符串中的所有位置?

ios - 如何在设备旋转后重新计算约束

NavigationBar 中的 iOS 11 Logo 和标题

iphone - 没有背景图片的 UINavigationBar 样式