PopOver TableView,Swift IOS 8 中单元格处的错误



我正在我的应用程序中使用ViewController。 在视图控制器中,我尝试使用文本字段弹出表视图。 但是有错误

这是我的代码弹出窗口:

   Func textFieldShouldBeginEditing(textField: UITextField) ->  Bool{      
    if (textField == paymentTextField){
        var paymentVC = MasterPaymentTableViewController()
        paymentVC.modalPresentationStyle = .Popover
        paymentVC.preferredContentSize = CGSizeMake(300, 500)
        let popOverPresentationVC = paymentVC.popoverPresentationController
        popOverPresentationVC?.delegate = self
        popOverPresentationVC?.permittedArrowDirections = .Down
        popOverPresentationVC?.sourceView = textField as UIView
       self.navigationController?.presentViewController(paymentVC, animated: true, completion: nil)
       return false
    }
  } 

代码处的此错误:

import UIKit
import CoreData
class MasterPaymentTableViewController: UITableViewController {
var myList : Array<AnyObject> = []
var appDel : AppDelegate!
var context : NSManagedObjectContext!
override func viewDidLoad() {
   super.viewDidLoad()

}
override func viewDidAppear(animated: Bool) {
   appDel = UIApplication.sharedApplication().delegate as! AppDelegate
   context = appDel.managedObjectContext
   let freq = NSFetchRequest(entityName: "PayMethod")
   myList = context.executeFetchRequest(freq, error: nil)!
   tableView.reloadData()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
   // Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
    let cellID : String = "Cell"
    // ERROR AT HERE : unexpectedly found nil while unwrapping an Optional value
    var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! UITableViewCell
    // -------------
    if let ip = indexPath {
    var myObject : NSManagedObject = myList[ip.row] as! NSManagedObject
    let title = myObject.valueForKeyPath("paymentName") as! String
    cell.textLabel!.text = title
}

return cell
}

但是,如果我直接从popOver打开表视图,它的工作正常

有什么解决办法吗?感谢

试试这个:

override func viewDidLoad() {
   super.viewDidLoad()
   tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}

并使用 dequeueReusableCellWithIdentifier:forIndexPath: 方法而不是使用 dequeueReusableCellWithIdentifier

let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath:indexPath)