您好,我已经在网上搜索了很长时间来解决这个问题,但我的 UITableViewController 中似乎有一个错误,当我滚动时,导航栏标题会根据我滚动的位置而改变。
更新:我包含了我的 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/