Dennis van Mazijk
Dennis van Mazijk

Reputation: 205

Swift - Second section in tableview not visible

I am trying to give my controller two grouped tableview sections. As of now, the first section and its header show up correctly, but the second one is nowhere to be found. Both sections are pretty straightforward and short, so they should perfectly fit within my frame.

Where did the second group go, and how can I make it appear below the first group?

import UIKit

struct SectionData {
   let title: String
   let data: [String]

var numberOfItems: Int {
    return data.count
}

subscript(index: Int) -> String {
    return data[index]
   }
}

extension SectionData {
  init(title: String, data: String...) {
    self.title = title
    self.data = data
   }
 }

class SettingsController: UITableViewController {

let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.tableView.isScrollEnabled = false
    self.tableView.tableFooterView = UIView(frame: CGRect.zero)
    self.tableView.backgroundColor = UIColor(white: 0.95, alpha: 1)
    self.tableView.rowHeight = 60
}

lazy var mySections: [SectionData] = {
    let section1 = SectionData(title: "INFORMATION", data: "About the app", "Our website", "Twitter", "Call us")
    let section2 = SectionData(title: "SUPPORT", data: "Report a problem", "Privacy Policy")

    return [section1, section2]
}()

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mySections[section].numberOfItems
}

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

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "Futura", size: 16)!
    header.textLabel?.textColor = UIColor.darkGray
}

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

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

    let cellTitle = mySections[indexPath.section][indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as UITableViewCell
    cell.selectionStyle = UITableViewCellSelectionStyle.default
    cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    cell.textLabel?.text = cellTitle

    return cell
   }
}

Upvotes: 0

Views: 1835

Answers (1)

Nirav D
Nirav D

Reputation: 72410

The signature of numberOfSections(in:) is changed in Swift 3, so change your method like way.

override func numberOfSections(in tableView: UITableView) -> Int {
    return mySections.count
}

Upvotes: 8

Related Questions