iOS: не работает несколько разделов в TableView - PullRequest
0 голосов
/ 03 октября 2018

Я работаю над приложением, в котором мне нужно показать несколько строк с заголовком.В моем случае только один раздел показывает.Я искал все, но не могу найти подходящее решение.

Here is my code:

class Timeline: UITableViewCell {
      @IBOutlet weak var timelineData: UITextView!
}

class StudenTimelineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let section = ["pizza", "deep dish pizza", "calzone"]

let items = [["Margarita", "BBQ Chicken", "Peproni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns & mashrooms"]]

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

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

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineId", for: indexPath) as! Timeline
    let gpsData = items[indexPath.section][indexPath.row]

    cell.timelineData.text = gpsData
    return cell
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}

What I am getting enter image description here

Какя получу все разделы.Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 03 октября 2018

Это потому, что имя вашего метода func numberOfSectionsInTableView(tableView: UITableView) -> Int неверно и поэтому не вызывается.

Замените имя на func numberOfSections(in tableView: UITableView) -> Int и увидите, как происходит волшебство.

0 голосов
/ 03 октября 2018

// MARK: - Источник данных табличного представления

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewCell", for: indexPath)
    cell.selectionStyle = UITableViewCellSelectionStyle.none

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension

}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension

}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    let cell: UITableViewCell
    cell = tableView.dequeueReusableCell(withIdentifier: "tableviewHeader")!

    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.backgroundColor = UIColor.white

    return cell
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 32;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...