【问题标题】:unexpected found nil on the line tableView.datasourse = self意外发现 nil 就行 tableView.datasource = self
【发布时间】:2015-12-19 05:32:12
【问题描述】:

我有一个导航控制器,并且我添加了一个自定义 UIBarButtonItem。我想要做的是当用户点击我的按钮时,它会显示一个 viewController,里面有一个 tableView。

为此,我写了这个:

let shopoingCarVC:shopingCartProductsList = shopingCartProductsList()
 self.navigationController?.pushViewController(shopoingCarVC, animated: true)

shopingCartProductsList 是我打算导航到的 ViewController,当它导航到它时,这行给了我意想不到的发现 nil 错误:

tableViewProducts.dataSource = self

我之前在我的其他 viewControllers 上做过,但是在没有 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
        }


  }

怎么了?

【问题讨论】:

  • 请确保您已将 tableview 数据源和委托方法连接到主视图并实现了所需的委托和数据源方法。
  • @AvijitNagare ,我编辑了我的问题。这是我到目前为止所做的
  • @sourav 我已经编辑了我的问题。检查完整的 ViewController 源代码。
  • 你可以使用断点检查产品是否包含值吗?故事板中的单元格标识符。
  • push 方法看起来 bug 使用前。 let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController self.navigationController?.pushViewController(secondViewController, animated: true) missing instantiateViewControllerWithIdentifier

标签: ios swift uitableview uinavigationcontroller


【解决方案1】:

在代码下方推送 viewcontroller 用户时使用错误。

let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController self.navigationController?.pushViewController(secondViewController, animated: true)  

在身份检查器中设置标识符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多