我有一个 navigationController,我添加了一个自定义 UIBarButtonItem。我想要做的是当用户点击我的按钮时,它会显示一个 viewController,其中有一个 tableView。
为此,我写了这个:
let shopoingCarVC:shopingCartProductsList = shopingCartProductsList()
self.navigationController?.pushViewController(shopoingCarVC, animated: true)
shopingCartProductsList 是我打算导航到的 ViewController,当它导航到它时,它在这一行给了我意外的 found nil 错误:
tableViewProducts.dataSource = self
我之前在我的其他 viewController 上做过,但是在没有 segue 和使用 pushViewController 方法的情况下导航时我遇到了这个问题。
这是我的 targetViewCOntroller :
import UIKit
import Alamofire
import Haneke
class shopingCartProductsList: BaseViewController ,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var ShopingCartProducts: UITableView!
var products = dataService.instance.shopingCartProduct
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
}
override func viewDidLoad() {
super.viewDidLoad()
ShopingCartProducts.dataSource = self
ShopingCartProducts.delegate = self
self.view.backgroundColor = COLOR_BACKGROUND
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let tblCell = tableView.dequeueReusableCellWithIdentifier("shopingCart_cell") as? shopingCart_cell {
if let prod = products[indexPath.row] as? productMD{
tblCell.configCell(prod)
}
return tblCell
}else{
return shopingCart_cell()
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("products count \(products.count)")
return products.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
}
怎么了?
最佳答案
将 viewcontroller 用户推送到代码下方时使用错误。
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController self.navigationController?.pushViewController(secondViewController, animated: true)
在身份检查器中设置标识符。
关于ios - 意外地在 tableView.datasource = self 行发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367624/