ios - 意外地在 tableView.datasource = self 行发现 nil

标签 ios swift uitableview uinavigationcontroller

我有一个 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)  

enter image description here

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

关于ios - 意外地在 tableView.datasource = self 行发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367624/

相关文章:

iphone - UITableView InsertRows (insertRowsAtIndexPaths) 仅对两个部分之一进行动画处理

ios - 在 iOS 上通过滑动对 UIImage 进行实时模糊

ios - 如何从另一个应用程序启动 iOS 照片应用程序? ( swift )

sqlite - 使用 swift + FMDB 插入 NULL

ios - 我如何在 View Controller 上保存和调用 UIImage?

iOS 7 TableView 类似于 iPad 上的设置应用程序

ios - 如何为通用 ios 设备构建 Webrtc ios?

ios - 停止推送 segue 直到被告知继续前进 - Obj C

ios - IOS CoreImage Framework是否支持模糊检测

ios - 使用 UITableViewAutomaticDimension 时 UIImageView 拉伸(stretch)不正确