Swift:TitleForHeaderInSection执行问题

时间:2016-11-18 19:15:44

标签: ios swift uitableview

我似乎无法做到正确,就调用命名我的标题而言。

如果你想知道我的模型是什么 - 它基本上是一个包含美丽地方的字典,按字母顺序书写。

愿任何人帮助我,因为我无法弄清楚我做错了什么?

import UIKit

class PlacesTableViewController: UITableViewController {

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return model.places.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0: return (model.places["A"]!.count)
        case 1: return (model.places["B"]!.count)
        case 2: return (model.places["C"]!.count)
        case 3: return (model.places["F"]!.count)
        case 4: return (model.places["G"]!.count)
        case 5: return (model.places["H"]!.count)
        case 6: return (model.places["M"]!.count)
        case 7: return (model.places["N"]!.count)
        case 8: return (model.places["P"]!.count)
        case 9: return (model.places["R"]!.count)
        case 10: return (model.places["S"]!.count)
        case 11: return (model.places["T"]!.count)
        case 12: return (model.places["V"]!.count)
        case 13: return (model.places["Y"]!.count)
        case 14 : return (model.places["Z"]!.count)
        default: return 1
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PlacesCell", for: indexPath)

        switch indexPath.section {
        case 0: cell.textLabel?.text = model.places["A"]?[indexPath.row]
        case 1: cell.textLabel?.text = model.places["B"]?[indexPath.row]
        case 2: cell.textLabel?.text = model.places["C"]?[indexPath.row]
        case 3: cell.textLabel?.text = model.places["F"]?[indexPath.row]
        case 4: cell.textLabel?.text = model.places["G"]?[indexPath.row]
        case 5: cell.textLabel?.text = model.places["H"]?[indexPath.row]
        case 6: cell.textLabel?.text = model.places["M"]?[indexPath.row]
        case 7: cell.textLabel?.text = model.places["N"]?[indexPath.row]
        case 8: cell.textLabel?.text = model.places["P"]?[indexPath.row]
        case 9: cell.textLabel?.text = model.places["R"]?[indexPath.row]
        case 10: cell.textLabel?.text = model.places["S"]?[indexPath.row]
        case 11: cell.textLabel?.text = model.places["T"]?[indexPath.row]
        case 12: cell.textLabel?.text = model.places["V"]?[indexPath.row]
        case 13: cell.textLabel?.text = model.places["Y"]?[indexPath.row]
        case 14: cell.textLabel?.text = model.places["Z"]?[indexPath.row]
        default: break
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
        switch section {
        case 0: cell?.textLabel?.text = "A"
        case 1: cell?.textLabel?.text = "B"
        case 2: cell?.textLabel?.text = "C"
        case 3: cell?.textLabel?.text = "F"
        case 4: cell?.textLabel?.text = "G"
        case 5: cell?.textLabel?.text = "H"
        case 6: cell?.textLabel?.text = "M"
        case 7: cell?.textLabel?.text = "N"
        case 9: cell?.textLabel?.text = "P"
        case 10: cell?.textLabel?.text = "R"
        case 11: cell?.textLabel?.text = "S"
        case 12: cell?.textLabel?.text = "T"
        case 13: cell?.textLabel?.text = "Y"
        case 14: cell?.textLabel?.text = "Z"
        default: break
        }

        return String(describing: cell)
    }
}

2 个答案:

答案 0 :(得分:0)

除此之外,您可能需要重新思考模型的工作原理。 titleForHeaderInSection只需要一个字符串,UIKit将处理其余部分。所以不要返回String(describing: cell)因为这只是单元格的名称。您需要返回您尝试将textLabel设置为的字符串。所以保持开关但是对于每种情况,而不是

cell?.textLabel?.text = "{Letter}"

这样做:

return "{Letter}"

并删除let cell ...行和最后一行

答案 1 :(得分:0)

你杀了我的小人物。 ;)

import UIKit

class PlacesTableViewController: UITableViewController {

    let letters = ["A", "B", "C", "F", "G", "H", "M", "N", "P", "R", "S", "T", "V", "Y", "Z"]

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return model.places.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let letter = letters[section]
        return model.places[letter]?.count ?? 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PlacesCell", for: indexPath)
        let letter = letters[indexPath.section]
        cell.textLabel?.text = model.places[letter]?[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let letter = letters[section]
        return letter
    }
}

修改

有关详细信息,请查看UITableViewDelegate

要编辑实际的标题视图,您需要专门使用viewForHeaderInSection