我在我的应用程序中使用ViewController。在ViewController中,我尝试使用textfield popOver tableview。但有错误
这是我的代码popUp:
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
}
但是,如果我直接打开tableView而不是popOver,它的工作正常
任何解决方案? THX
答案 0 :(得分:0)
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
并使用dequeueReusableCellWithIdentifier:forIndexPath:
方法而不是dequeueReusableCellWithIdentifier
:
let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath:indexPath)