swiftuitableviewuiviewside-menu

How to add view in side menu table in swift


I have created side menu table with width 200, there i am able to add label to show menu items but here i have to add uiview above the label in table in swift.

Here is my code:

import UIKit

class MenuTableViewController: UITableViewController {

var selectedMenuItem : Int = 0

var menuItems = ["Home", "SignIN", "SignUp", "QR Code", "CreateBUsiness", "Services", "Employees", "Settings", "EmployeeTimeoff" ,"Billing", "Raise Request", "MYBusiness"]


override func viewDidLoad() {
    super.viewDidLoad()


    //Customize apperance of table view
    tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    tableView.separatorStyle = .none
    tableView.backgroundColor = UIColor.blue
    tableView.scrollsToTop = false


    //Preserve selection between presentations
    self.clearsSelectionOnViewWillAppear = false

    tableView.selectRow(at: IndexPath(row: selectedMenuItem, section: 0), animated: false, scrollPosition: .middle)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    // Return the number of sections.
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    return menuItems.count
}

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

    var cell = tableView.dequeueReusableCell(withIdentifier: "CELL")

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "CELL")
        cell!.backgroundColor = UIColor.clear

        cell!.textLabel?.textColor = UIColor.white

    }

    cell!.textLabel?.text = menuItems[indexPath.row]//"ViewController #\(indexPath.row+1)"

    return cell!
}

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

The out put is

enter image description here

But i need like this:

enter image description here

Please help me to solve this issue.


Solution

  • You can add a header for the tableview by function func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {}

    You can custom and add anything in this header. Please try and let me know your next issue