tableView中的节不显示

时间:2017-09-12 13:58:48

标签: swift uitableview

我已经构建了下面的代码,但只有'第1节'标题加元素出现。我希望所有3个标题都显示在下面的相应元素,为什么不发生这种情况?

import Foundation
import UIKit



class summary: UIViewController, UITableViewDelegate, UITableViewDataSource  {


    var itemsInSections: Array<Array<String>> = [["1A", "1B", "1C"], ["2A", "2B"], ["3A", "3B", "3C", "3D", "3E"]]
    var sections: Array<String> = ["Section 1", "Section 2", "Section 3"]



    @IBOutlet var tableView: UITableView!



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

        let cell:TableViewCell1 = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell1


        cell.label.text = "\(itemsInSections[indexPath.section][indexPath.row])"
        return cell
    }




    override func viewDidLoad() {
        let nib = UINib(nibName: "tblcell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: "cell")

        self.tableView.dataSource = self
        self.tableView.delegate = self

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemsInSections[section].count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.sections[section]
    }





}

2 个答案:

答案 0 :(得分:0)

替换它: -

  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.sections.count
    }

用这个: -

 func numberOfSections(in tableView: UITableView) -> Int {
                return self.sections.count

   }

答案 1 :(得分:0)

方法numberOfSection签名是:

optional func numberOfSections(in tableView: UITableView) -> Int

如果没有设置(因为你的签名没有匹配),默认情况下应该返回1个部分。这就是为什么你只看到一个部分的原因。 在您的确认下,您的版本没有被调用。