ios - 如何设置一个整数值作为导航栏的标题?

标签 ios swift uitableview uinavigationbar

本质上,我想将变量的值设置为导航 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/

相关文章:

iphone - 以编程方式通过 Xcode iPhone 中的本地化更改应用程序图标和应用程序名称

ios - Swift 解析不更新核心数据

ios - 将隐藏的 UIITextField 添加到 MFMailComposeViewController

html - iOS Safari 在视觉上为单选选择多个选项

ios - GADInterstitialAd 导致内存问题

iOS AutoLayout 工作出现问题

swift - 在 Swift 中初始化字典的不同方法?

ios - Swift:将 searchBar 固定为 tableView header

iphone - 从 UITableview 向上滚动时的操作

ios - 尝试快速将选定的 tableview 单元格值传递给 NavigationBar 标题