簡體   English   中英

UITableView委托方法未在Swift 3中調用

[英]UITableView delegate method not being called in swift 3

我有一個被繼承和擴展的表視圖,然后在視圖控制器中進行設置。 我遇到的問題是未調用委托方法ViewForHeaderInSection,而正在調用普通的數據源方法。

(這是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
    }

}

這是View控制器代碼:

class ViewController: UIViewController {


    @IBOutlet weak var myTableView: MyTableView!

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

}

為什么沒有調用委托方法的任何特定原因? 預先感謝您的寶貴時間。

為此,您還必須實現一個另外的方法heightForHeaderInSectionviewForHeaderInSection方法。

如果您使用的是viewForHeaderInSection委托方法,則必須使用heightForHeaderInSection方法,否則不會調用其他明智的節標題方法

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

官方鏈接以獲取更多描述https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614855-tableview

在viewDidLoad中添加:

tableView.estimatedSectionHeaderHeight = 80

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM