TableView resizing - only when button clicks

Question!

I am having some issues with my table view resizing - As it looks when the page loads :

enter image description here

And then once the button is clicked

enter image description here

As you can see, the button click renders the table to be of the correct size - how can I achieve this from the outset - as it makes it look much neater

My code for the class is as follows;

//
//  FirstViewController.swift
//  Intercultural Collaboration
//
//  Created by Ricki Lambert on 06/10/2014.
//  Copyright (c) 2014 com.kentapps. All rights reserved.
//

import UIKit

class QuizViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate{

    var countries:Array<Answer> = [];
    var selected = -1;
    var positionOfSelected = 1;

    @IBOutlet var next: UIBarButtonItem!
    @IBOutlet var tableView: UITableView!


    @IBAction func next(sender: AnyObject) {

    }

    func checkOkToProceed() -> Bool {
        positionOfSelected = 1;
        var result = false;

        for answer in countries{
            if(answer.selected){
                result = true;
                break;
            }
            positionOfSelected++;
        }
        return result;
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

        if(sender.tag > 0 && !self.checkOkToProceed()){
            //the user has not selected an answer so
            //we need to show them a prompt

            var alert = UIAlertController(title: "Unanswered Question", message: "Please select an answer!", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil);
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad();

        tableView.reloadData();
        //load answers to countries array
        self.tableView.allowsMultipleSelection = false;

    }

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



    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

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

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

        let cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = self.countries[indexPath.row].answerName;
        cell.textLabel?.textAlignment = NSTextAlignment.Center;

        let countryImage = String(self.countries[indexPath.row].answerName) + ".png";
        cell.imageView?.image = UIImage(named: countryImage);


        if(countries[indexPath.row].selected){
            let imageName = "tick.png";
            let image: UIImageView = UIImageView(image: UIImage(named: imageName));
            cell.accessoryView = image;
        }else{
            let image: UIImageView = UIImageView();
            cell.accessoryView = image;
        }

        tableView.sizeToFit();
        return cell;
    }


    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

        countries[indexPath.row].setSelected(true);

        if(selected != -1){
            countries[selected].setSelected(false);
        }
        selected = indexPath.row;
        tableView.reloadData();
    }

    func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {

        if (buttonIndex == 0) {
            //Do something
        }
    }

}


Answers

I recently had this issue. Here is an elegant solution:

tableView.tableFooterView = UIView()
By : Mundi


This video can help you solving your question :)
By: admin