Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift使用其他控制器的视图作为uitableview的背景_Uitableview_Swift - Fatal编程技术网

Swift使用其他控制器的视图作为uitableview的背景

Swift使用其他控制器的视图作为uitableview的背景,uitableview,swift,Uitableview,Swift,我是swift新手,正在尝试创建一个具有表视图的应用程序,该表视图可以使用来自另一个UIView控制器的视图 这可行吗? 如果是这样,请帮助我,我已经附上了我的代码,主要控制器如下。如果有什么问题,请告诉我? 非常感谢你 import UIKit class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet weak var tableView: UITableView

我是swift新手,正在尝试创建一个具有表视图的应用程序,该表视图可以使用来自另一个UIView控制器的视图

这可行吗? 如果是这样,请帮助我,我已经附上了我的代码,主要控制器如下。如果有什么问题,请告诉我? 非常感谢你

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!
var numbers:[String] = []
let vc = bgViewController()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    tableView.backgroundView = vc.view
}

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 {
    if(numbers.count == 0){
        tableView.backgroundView?.hidden = false
        println("1")
    }
    else{
        tableView.backgroundView?.hidden = true
    }
    return numbers.count
}

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

    return cell
}


}

在attribure inspector中提供脚本标识符作为bgViewController。

您不应该像let vc=bgViewController那样创建vc。可以使用nibname方法,也可以通过故事板进行实例化。谢谢您的帮助。我在故事板中创建了另一个viewController,并将该类更改为bgViewController,我为其创建了一个单独的文件。这对吗?非常感谢!但是这给了我一个错误,说类ViewController没有初始化器。将let转换为var.var vc:bgViewController!现在试试,谢谢!它会抓取另一个viewController的视图。还有一个问题,如果我在另一个视图中添加一个按钮,它是否可以在表视图中单击?我希望不会,但您仍然可以尝试。
import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!
var numbers:[String] = []
var vc: bgViewController!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
vc = self.storyboard.instantiateViewControllerWithIdentifier("bgViewController") as! bgViewController

    tableView.backgroundView = vc.view
}

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 {
    if(numbers.count == 0){
        tableView.backgroundView?.hidden = false
        println("1")
    }
    else{
        tableView.backgroundView?.hidden = true
    }
    return numbers.count
}

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

    return cell
}


}