-1

I am learning to make IOS Programs using Swift and Xcode. I am reading a book as well as watching videos. I ran into a problem. I created a Table View into my view controller. I know how to specify the amount of rows and what goes into the text labels. However, I don't know how to remove the extra lines that tableview produces. I only have 4 rows but it shows the blank extra rows.

Here is my code:


import UIKit /// imports the UIKit library

class MainScreenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var MainScreenTableView: UITableView!
    let myMenuOptions = [ "Instructions", "Create", "Open", "Send" ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.MainScreenTableView.dataSource = self // self refers to the view controler we are in
        self.MainScreenTableView.delegate = self   /* dataSource means the view control provides the table with information */
    }

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell()
        cell.textLabel!.text = myMenuOptions[indexPath.row]
        return cell
    }

thanks

Rob
  • 415,655
  • 72
  • 787
  • 1,044
dancoder
  • 1
  • 1

2 Answers2

1

This is kind of an annoying thing about UITableView, but is pretty easy to fix. You just need to implement the following two methods as so:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

Or in Swift

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.min
}

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView()
}
oltman
  • 1,772
  • 1
  • 14
  • 24
0

You need to add an empty footer to the table view to remove the extra rows. In viewDidLoad, add the following code:

let footer = UIView(frame: CGRectZero)
self.MainScreenTableView.tableFooterView = footer

See UITableView documentation for more information on table view footers.

tktsubota
  • 9,371
  • 3
  • 32
  • 40