本质上,我想将变量的值设置为导航 Controller 中显示的标题。该变量称为 titleAmount
,应该表示 TableView 中的行数。
为此,我创建了变量 var titleAmount:String?
在 numberOfRowsInSection
中,我添加了以下内容:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let theIntegerValue :Int = feedItems.count
let theStringValue :String = String(theIntegerValue)
titleAmount = theStringValue
return feedItems.count
}
因为标题必须是一个字符串,而我的变量正在寻找一个字符串,所以我在上面添加了以下内容:
let theIntegerValue :Int = feedItems.count
let theStringValue :String = String(theIntegerValue)
将feedItems.count
的值转换成字符串
^^ This is probably where the issue is ^^
feedItems.count
显示我的表格 View 中的行数。 (我已经测试并确认它有效)
最后,在 viewDidLoad()
中,我添加了 self.navigationItem.title = titleAmount 以将变量的值设置为导航栏标题。
没有显示该字段留空。什么问题?
最佳答案
您不应尝试更新 numberOfRowsInSection
中的标题。在填充或更新 feedItems
的任何位置更新标题。
在 feedItems
中更新项目数量的任何地方都需要更新标题:
self.navigationItem.title = "\(feedItems.count)"
请记住,即使您在 viewDidLoad
中执行了 self.navigationItem.title = titleAmount
,对 titleAmount
的任何进一步更改都不会将反射(reflect)在标题中。
关于ios - 如何设置一个整数值作为导航栏的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046887/