TableView调整大小 - 仅在按钮单击时

时间:2014-10-15 11:49:54

标签: ios objective-c uitableview swift

我在调整表格视图时遇到了一些问题 - 正如页面加载时所看到的那样:

enter image description here

然后点击按钮后

enter image description here

正如您所看到的,单击按钮会使表格大小正确 - 我如何从一开始就实现这一目标 - 因为它使它看起来更整洁

我的课程代码如下:

//
//  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
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我最近遇到过这个问题。这是一个优雅的解决方案:

tableView.tableFooterView = UIView()