Ravi
Ravi

Reputation: 960

iOS: Multiple Section in TableView not working

I am working on an app in which I need to show multiple rows with a header. In my case only one section is showing. I have searched everything but can't find a suitable solution.

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

How will I get all the sections. Thanks in advance.

Upvotes: 1

Views: 830

Answers (2)

iOSer
iOSer

Reputation: 2326

This is because your method name func numberOfSectionsInTableView(tableView: UITableView) -> Int is incorrect and hence not called.

Replace the name with func numberOfSections(in tableView: UITableView) -> Int and see the magic happen.

Upvotes: 6

Maulik Salvi
Maulik Salvi

Reputation: 279

//MARK: - Table view data source

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;
}

Upvotes: 0

Related Questions