2017-07-31 12 views
2

サブクラス化されて拡張されたテーブルビューがあり、Viewコントローラに設定されています。私が持っている問題は、通常のデータソースメソッドが呼び出されている間、ViewForHeaderInSectionが呼び出されていないデリゲートメソッドです。switでUITableViewデリゲートメソッドが呼び出されていません3

(これはテーブルビューの設定方法である、のviewDidLoadで呼び出されます。テーブルビューがIBOutletとビューコントローラに接続されている)

func setup() { 
    self.dataSource = self as UITableViewDataSource 
    self.delegate = self 
    let nib = UINib(nibName: "MyTableViewCell", bundle: nil) 
    self.register(nib, forCellReuseIdentifier: "MyCell") 

} 

これらの拡張機能

extension MyTableView: UITableViewDataSource,UITableViewDelegate { 


    func numberOfSections(in tableView: UITableView) -> Int { 
     //print(Genres.total.rawValue) 
     return Genres.total.rawValue 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if let tableSection = Genres(rawValue: section), let articleData = articleDictionary[tableSection] { 
      // print(articleData.count) 
      return articleData.count 
     } 
     print(0) 
     return 0 
    } 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyTableViewCell 

     if let tableSection = Genres(rawValue: indexPath.section), let article = articleDictionary[tableSection]?[indexPath.row]{ 
      cell.cellTitle.text = article.articleTitle 
      cell.cellImageView.image = article.articleImage 
      cell.cellEmojiReaction.text = article.articleEmojis 
     } 


     return cell 
    } 


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 40.0)) 
     view.backgroundColor = .brown 
     let label = UILabel(frame: CGRect(x: 16, y: 0, width: tableView.bounds.width, height: 40)) 
     label.textColor = .blue 

     let tableSection = Genres(rawValue: section) 
     switch tableSection { 
     case .breakingNews?: 
      label.text = "Breaking News" 
     case .scienceAndTech?: 
      label.text = "Science and Tech" 
     case .entertainment?: 
      label.text = "Entertainment" 
     case .sports?: 
      label.text = "Sports" 
     default: 
      label.text = "Invalid" 
     } 
     print("Function Run") 
     view.addSubview(label) 
     print(label.text ?? "Nothing Here") 
     return view 
    } 

} 

ある。ここでコントローラを表示するコード:

class ViewController: UIViewController { 


    @IBOutlet weak var myTableView: MyTableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     myTableView.setup() 
    } 

} 

具体的な理由はありますか?デリゲートメソッドが呼び出されていませんか?あなたの時間のために事前にありがとうございます。

+0

のための公式のリンクを私の答えを確認してください。 –

答えて

1

viewForHeaderInSectionメソッドと一緒にもう1つの方法heightForHeaderInSectionを実装する必要があります。

+0

偉大な、ありがとう! –

0

のviewDidLoadに追加します。

tableView.estimatedSectionHeaderHeight = 80 
0

あなたはviewForHeaderInSectionデリゲートメソッドを使用している場合、他の賢明なセクションヘッダmehtodが以前のiOS 5.0、テーブルに

と呼ばれていないheightForHeaderInSectionメソッドを使用することが義務付けられていますビューは、 tableView(_:viewForHeaderInSection :)がnilビューを返したセクションのヘッダーの高さを0からに自動的にリサイズします。 iOS 5.0 以降では、この方法で各セクションヘッダ の実際の高さを返す必要があります。

以上説明https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614855-tableview

関連する問題