Programmatically create a grouped UITableView

I have a UITableViewController that currently displays 4 cells that will always be displayed, and would like to group them together, but I cannot figure out how to do this. The resources I found give only instructions using Interface Builder when a standard UITableView is pasted on top of a UIViewController or something like that. How can I group them programmatically?

Here is my current UITableViewController class:

import UIKit

class SecondViewController: UITableViewController {

    let Items = ["Altitude","Distance","Groundspeed"]

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.Items.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        cell.textLabel?.text = self.Items[indexPath.row]
        return cell
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

}
+5
source share
1 answer

, viewDidLoad():

    self.tableView = UITableView(frame: self.tableView.frame, style: .grouped)

, numberOfSectionsInTableView(_:) , tableView(_:titleForHeaderInSection:) tableView(_:titleForFooterInSection:).

+16

Source: https://habr.com/ru/post/1569237/


All Articles