Ios TableView调整大小-仅当按钮单击时
我在调整表视图的大小时遇到了一些问题-正如页面加载时所示: 然后点击按钮 如您所见,单击按钮将使表格具有正确的大小-我如何从一开始就做到这一点-因为它使表格看起来更整洁 我的课程代码如下Ios TableView调整大小-仅当按钮单击时,ios,objective-c,uitableview,swift,Ios,Objective C,Uitableview,Swift,我在调整表视图的大小时遇到了一些问题-正如页面加载时所示: 然后点击按钮 如您所见,单击按钮将使表格具有正确的大小-我如何从一开始就做到这一点-因为它使表格看起来更整洁 我的课程代码如下 // // FirstViewController.swift // Intercultural Collaboration // // Created by Ricki Lambert on 06/10/2014. // Copyright (c) 2014 com.kentapps. All rig
//
// 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
}
}
}
我最近有这个问题。以下是一个优雅的解决方案:
tableView.tableFooterView = UIView()
我有点不明白为什么在cellForRowAtIndexPath中每次创建单元格后都调用tableView.sizeToFit?您是否尝试在tableView.reloadData之后调用viewDidLoad中的sizeToFit?是的,但它不起作用。很遗憾,尝试在viewDidLoad中调用sizeToFit似乎会有什么不同。这很有效,除了您短暂地看到底部的一点,然后它消失了使用视图将出现您是否将其放入viewDidLoad?我有-是-在super.viewDidLoad之后您是否消除了sizeToFit?-作为另一种选择,您可能需要考虑基于单元高度和国家数来计算表视图的期望帧。