意外发现nil就行tableView.datasourse = self

时间:2015-12-19 05:32:12

标签: ios swift uitableview uinavigationcontroller

我有一个navigationController,我添加了一个自定义的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
        }


  }

怎么了?

1 个答案:

答案 0 :(得分:0)

在将codecontroller用户推送到代码下方时使用错误。

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

enter image description here

在Identity检查器中设置标识符。