0

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.

Swift
  • 1,074
  • 12
  • 46
  • in tableView menu add header view.just search how to add header view you will get lots of work example.https://stackoverflow.com/questions/31964941/swift-how-to-make-custom-header-for-uitableview – channu Jul 30 '19 at 16:23
  • @channu, thank you so much, its working. thanks for the link also, its worked for me. – Swift Jul 31 '19 at 17:11
  • WC..Happy coding – channu Aug 01 '19 at 05:21

3 Answers3

0

One of the ways to achieve this is by adding a custom view to the table view header.

This can be done by overriding viewForHeaderInSection in MenuTableViewController.

Here is how:

  1. Create a custom view using xib file, let's say "TableViewHeader.xib"
  2. Override viewForHeaderInSection in MenuTableViewController.
  3. Get a reference to the xib file and return it in the viewForHeaderInSection function.

In the MenuTableViewController class it should look like this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let tableHeaderView = Bundle.main.loadNibNamed("TableViewHeader", owner: self, options: nil)?.first! as! UIView

    return tableHeaderView
}
shbedev
  • 1,875
  • 17
  • 28
0

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

0

I also have this kind of problems in adding table views in swift . so I follow this link and there are all the details clearly . so follow this enter link description here