2017-07-31 73 views
2

我有一個子視圖和擴展的表視圖,然後在View控制器中進行設置。我遇到的問題是委託方法ViewForHeaderInSection沒有被調用,而正在調用正常的數據源方法。在swift中沒有調用UITableView委託方法3

(這是TableView中設置方法,被稱爲在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

爲此,您還必須實施另一種方法heightForHeaderInSection以及viewForHeaderInSection方法。

+0

太好了,謝謝! –

0

添加在viewDidLoad中:

tableView.estimatedSectionHeaderHeight = 80 
0

如果您正在使用viewForHeaderInSection委託方法則是強制性使用heightForHeaderInSection方法其他明智節頭mehtod不叫

此前的iOS 5.0,表對於 tableView(_:viewForHeaderInSection :)返回nil視圖的部分,視圖會自動調整標頭的高度 爲0。在iOS 5.0 及更高版本中,您必須以此方法返回每個節標題 的實際高度。

更多說明官鏈接https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614855-tableview

相關問題