Swift 弹出窗口中的表格视图不显示数据

Swift tableview in popover doesn't show data

我正在尝试设置一个包含文本字段和表格视图的弹出视图,但我无法让表格视图显示数据。如果你能帮助我,我将不胜感激。

  1. 在主ViewController上,我放了一个标签来触发popover的segue,

    import UIKit
    
    class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    
    @IBOutlet weak var textField: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func popover(sender: AnyObject) {
        self.performSegueWithIdentifier("ShowDetails", sender: self)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {        
        if segue.identifier == "ShowDetails" {
            if let controller = segue.destinationViewController as? UIViewController {
                controller.popoverPresentationController!.delegate = self
                controller.preferredContentSize = CGSize(width: 320, height: 50)
            }
        }
    
    }
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return .None
    }
    }
    
  2. 为原型单元设置了 PopoverCell,

        import UIKit
    
    class PopoverCellTableViewCell: UITableViewCell {
    
    
    @IBOutlet weak var AreaCellLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
    }
    }
    
  3. 为弹出窗口本身设置了一个 PopoverControllerView。

    import UIKit
    
    class PopoverViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
    
    var areas = [Area]()
    
       // @IBOutlet weak var NewArea: UITextField!
    
       // @IBOutlet weak var SaveNewArea: UIButton!
          @IBOutlet weak var subtableView: UITableView!
    
    
    override func viewDidLoad() {
    
    subtableView.dataSource = self
    subtableView.delegate = self
    
    super.viewDidLoad()
    
    LoadsampleArea()
    
    // Do any additional setup after loading the view.
    }
    
    func LoadsampleArea () {
    
    let area1 = Area(AreaName:"Moountain")!
    let area2 = Area(AreaName:"ByHill")!
    let area3 = Area(AreaName:"Yard")!
    
    areas += [area1, area2, area3]
    
    }
    
    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 areas.count
    }
    
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a     cell identifier.
        let cellIdentifier = "AreaCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PopoverCellTableViewCell
    
    let area = areas[indexPath.row]
    
    cell.AreaCellLabel.text = area.AreaName
    dismissViewControllerAnimated(true, completion:nil)
    return cell
    }
    
    }
    
  4. 和一个简单的数据文件来放置数据。

    import UIKit
    
    class Area {
    
    var AreaName: String
    
    init? (AreaName: String) {
    self.AreaName = AreaName
    
        if AreaName.isEmpty {
        return nil
        }
    
    }
    }
    

您的 UITableView 对象在您的 PopoverViewController class 中在哪里?我看不到任何参考。

也许你没有复制粘贴它,因为文本字段也被注释了,在这种情况下,我建议检查是否设置了委托数据源 属性。