将字符串数组传递给 UITableView 标头



我在将Array<String>[String]传递给UITableView标头部分名称时遇到问题。

UITableHeaders = [
    "Current Part Number",
    "Work Center",
    "Cycle Time",
    "Current Part Cycle Time",
    "Parts Made",
    "Cycle Time Actual",
    "Target",
    "Downtime",
    "Status Reason",
    "Line Status",
    "Production Effeciency",
    "Plus Minus (From Target)",
    "Current Production Time"
]

第一次尝试:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let titlesForHeadersArray: [String] = varStorage().UITableHeaders
    let titlesCount = titlesForHeadersArray.count
    return titlesForHeadersArray[]
}

第二次尝试:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let titlesForHeadersArray: [String] = varStorage().UITableHeaders
    let titlesCount = titlesForHeadersArray.count
    return titlesForHeadersArray[0...titlesCount]
}

这两个是我尝试用我存储的数组值填充标头的两次尝试。任何帮助将不胜感激。

Error: Ambiguous subscript with base type '[String]' and index type 'ClosedRange<Int>'

你可以尝试在titleForHeaderInSection

titleForHeaderInSection 向数据源询问表视图指定节的标题。

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let titlesForHeadersArray: [String] = varStorage().uiTableHeaders
    return titlesForHeadersArray[section]
}

并且您的 numberOfSection 应该返回您的数组计数

numberOfSections 要求数据源返回表视图中的部分数。

func numberOfSections(in tableView: UITableView) -> Int {
    return varStorage().uiTableHeaders.count
}

函数 TitleForHeaderInSection 等待您返回字符串,同时尝试返回字符串数组。尝试更改返回标题ForHeadersArray[0...titlesCount]以返回titleForHeadersArray[section]

最新更新