表视图显示空白屏幕

时间:2014-07-31 23:01:35

标签: xcode swift

我遇到了显示空白屏幕的TableViewController的问题。我刚刚改变了我的个性化'从ViewController到TableViewController的屏幕,但是当点击个性化单元格并且应用程序进入个性化屏幕时,显示的所有内容都是空白屏幕。

这是一张图片,展示了它应该如何以及如何在右侧看起来:

enter image description here

运行时的外观如下:

enter image description here

内容设置为静态,样式设置为Grouped。

这是我目前唯一的代码,因为我还没有完成将代码从旧版本转换到新版本,因为我遇到了这个问题......

import UIKit

class PersonalisationTableViewController: UITableViewController, UIPickerViewDelegate {

@IBOutlet weak var changeButton: UIButton!
@IBOutlet weak var userNameTextField: UITextField!

let coloursArray = ["Default (Blue)", "Green", "Orange", "Purple", "Red", "Turquoise"]

@IBAction func userNameChange(sender : AnyObject) {
    //Closes keyboard when user touches enter button

    userNameTextField.resignFirstResponder()
    globalUserName = userNameTextField.text
    globalUserName = globalUserName.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    userNameTextField.placeholder = globalUserName
    userNameTextField.text = ""
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0
}

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0
}

/*
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell

    // Configure the cell...

    return cell
}
*/

/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView!, moveRowAtIndexPath fromIndexPath: NSIndexPath!, toIndexPath: NSIndexPath!) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    // Return NO if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

}

我没有多次使用表格视图,所以我确定问题很明显,但我无法弄清楚。

2 个答案:

答案 0 :(得分:2)

这很可能是因为您的numberOfSectionsInTableView和numberOfRowsInSection方法返回0.您在故事板中设置了UITableView,但这两种方法都会覆盖它。如果UITableView中有静态单元格,则不应覆盖这两种方法。当您将类创建为UITableViewController的子类时,它们可能会遗留下来。

这是基本上发生的事情:你的UITableViewController连接到你的PersonalisationTableViewController文件。当您的PersonalisationTableViewController为tableView设置部分和行时,它将部分设置为0,并将每个部分的行设置为0.

我刚尝试将这两种方法放入我的一个有静态单元格的应用程序中,我可以确认它看起来是空白的。

如果您以编程方式创建视图,则可以执行以下操作:

   override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    // Return the number of sections.
    return 2
}

   override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    return 1

     // Or this if you had a different number rows for each section:

        if section == 0 {
            return //someInt
        } else if section == 1 {
            return //someOtherInt
        } else {
            //......
        }

}

编辑:另外,我相信这是Xcode的一个错误,但您必须覆盖:

    override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    if indexPath.section == 0 {
        return someHeight
    } else if indexPath.section == 1 {
        return someOtherHeight
    } else {
        // ....
    }
}

答案 1 :(得分:1)

此错误有一个非常简单的修复:

选择ViewController场景。现在,从侧栏选项中选择检查器面板。选中“是初始视图控制器”属性选项,保存项目并再次构建项目。

相关问题