2016-11-18 18 views
0

私のヘッダーの名前を付けることが重要である限り、それは正しくできないようです。Swift:TitleForHeaderInSection実行時の問題

私のモデルが何であるか疑問に思っているのなら、それは基本的に美しい場所を含む辞書でアルファベット順に書かれています。

私が間違っていることを本当に理解できないので誰でも助けてくれますか?

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) 
    } 
} 
+0

UITableViewヘッダービューはUITableViewHeaderFooterView型であり、UITableViewCellではありません。 dequeReuseableCellを使用するのではなく、ペン先からロードする必要があります。または、UICollectionReusableViewの形式で再利用可能なヘッダー/フッタービューを持つUICollectionViewを使用します。 –

+0

@JoshHomann OPは 'viewForHeader'ま​​たは' viewForFooter'を使用していません。 OPは 'titleForHeader ...'を使用しているので、単純な文字列だけが必要です。 – rmaddy

答えて

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 
    } 
} 

EDIT:

はさらに情報のためUITableViewDelegateを見てください。

実際のヘッダービューを編集するには、具体的にはviewForHeaderInSectionを使用します。

+0

ありがとうございます! :) フォローアップの質問に気をつけてもよろしいですか?セクションをどのようにスタイルするのですか?フォントサイズの変更や背景の変更など? –

+0

問題ありません。私の編集をチェックしてください。 – Frankie

0

実際には、モデルの仕組みを再考する必要があります。 titleForHeaderInSectionは文字列を必要とし、UIKitは残りを処理します。だからString(describing: cell)原因を返すことはありませんそれはちょうどセルの名前になります。 textLabelを設定しようとしている文字列を返す必要があります。だから、スイッチを維持するが、それぞれのケースについて、代わりに

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

これを行うの:

return "{Letter}"

をし、LET細胞...ラインと最後のリターンラインを削除