gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:16 25 4
gpt4 key购买 nike

您好,我已经在网上搜索了很长时间来解决这个问题,但我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com