在swift 3中没有调用UITableView委托方法

时间:2017-07-31 03:43:17

标签: ios swift uitableview

我有一个子视图和扩展的表视图,然后在View控制器中设置。我遇到的问题是没有调用委托方法ViewForHeaderInSection,而正在调用普通的数据源方法。

(这是TableView设置方法,在ViewDidLoad中调用。表视图通过IBOutlet连接到View Controller)

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

}

委托方法没有被调用的具体原因是什么?提前谢谢您的时间。

3 个答案:

答案 0 :(得分:1)

为此,您还需要实施另外一种方法heightForHeaderInSection以及viewForHeaderInSection方法。

答案 1 :(得分:0)

如果您使用的是viewForHeaderInSection委托方法,则必须使用heightForHeaderInSection方法,其他明智的部分标题mehtod不会被调用

  

在iOS 5.0之前,表格视图会自动调整高度   对于其中的部分,标题为0   tableView(_:viewForHeaderInSection :)返回一个nil视图。在iOS 5.0中   然后,您必须返回每个节标题的实际高度   在这种方法中。

官方链接了解更多说明https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614855-tableview

答案 2 :(得分:-1)

在viewDidLoad中添加:

tableView.estimatedSectionHeaderHeight = 80