2017-06-30 57 views
0

我已经在ViewController中以编程方式创建了tableView,但是当我加载ViewController时,函数cellForRowAtIndexPath未被调用。CellForRowAt indexPath未被调用iOS Swift

我看看StackOverflow上许多建议,但没有任何帮助的解决此问题。请有人请指导我哪里出错。

这里是我的代码:

import UIKit 
import AlgoliaSearch 

class ReportVC: UIViewController, UITableViewDelegate, UITableViewDataSource, DrugMoleculesTVCProtocol { 

    // FOR SCREEN DIMENSIONS 
    let screenSize: CGRect = UIScreen.main.bounds 
    var screenWidth: CGFloat 
    var screenHeight: CGFloat 
    var saveButton : UIBarButtonItem? 
    var cancelButton: UIBarButtonItem? 

    // For Table view 
    var tableView : UITableView  = UITableView() 
    let cellIdentifier = "Cell" 
    var tableViewCell : UITableViewCell = UITableViewCell() 
    var selectedObjectsArray: Array = [Any]() 

    required init?(coder aDecoder: NSCoder) { 
     screenHeight = screenSize.height 
     screenWidth = screenSize.width 
     super.init(coder: aDecoder) 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Setting buttons for the Nav Bar 
     saveButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.save, target: self, action: #selector(save)) 
     cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(cancel)) 
     self.navigationItem.rightBarButtonItem = saveButton 
     self.navigationItem.leftBarButtonItem = cancelButton 
     self.edgesForExtendedLayout = [] // This ensures that the views dont go below the navigation bar 


     // ADDING LABELS AND TEXT VIEWS 
     let myObservationsLabel : UILabel = { 
      let label = UILabel() 
      label.text = "My Observations" 
      return label 
     }() 

     let myObservationsTextView : UITextView = { 
      let textView = UITextView() 
      textView.backgroundColor = UIColor.lightGray 
      textView.font?.withSize(22.0) 
      return textView 
     }() 


     let myLabel : UILabel = { 
      let label = UILabel() 
      label.text = "Standard" 
      return label 
     }() 

     let addButton : UIButton = { 
      let button = UIButton() 
      button.setTitle("Add", for: .normal) 
      button.setTitleColor(UIColor.darkGray, for: .normal) 
      button.addTarget(self, action: #selector(addItems), for: .touchUpInside) 
      return button 
     }() 

     let myTextField : UITextField = { 
      let textField = UITextField() 
      textField.placeholder = "Name" 
      return textField 
     }() 


     let nextStepsLabel : UILabel = { 
      let label = UILabel() 
      label.text = "Next Steps" 
      return label 
     }() 

     // Add textView for Next steps 
     let nextStepsTextView : UITextView = { 
      let textView = UITextView() 
      textView.backgroundColor = UIColor.lightGray 
      textView.font?.withSize(22.0) 
      return textView 
     }() 

     self.view.addSubview(myObservationsLabel) 
     self.view.addSubview(myObservationsTextView) 
     self.view.addSubview(myLabel) 
     self.view.addSubview(addButton) 


     tableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain) 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier) 

     // Setting datasource and Delegate for TableView 
     tableView.delegate  = self 
     tableView.dataSource = self 
     self.view.addSubview(tableView) 

     self.view.addSubview(myTextField) 
     self.view.addSubview(nextStepsLabel) 
     self.view.addSubview(nextStepsTextView) 

     // Disabling Auto Constraints to set constraints manually 
     myObservationsLabel.translatesAutoresizingMaskIntoConstraints = false 
     myObservationsTextView.translatesAutoresizingMaskIntoConstraints = false 
     myLabel.translatesAutoresizingMaskIntoConstraints    = false 
     addButton.translatesAutoresizingMaskIntoConstraints    = false 
     tableView.translatesAutoresizingMaskIntoConstraints    = false 
     myTextField.translatesAutoresizingMaskIntoConstraints   = false 
     nextStepsLabel.translatesAutoresizingMaskIntoConstraints   = false 
     nextStepsTextView.translatesAutoresizingMaskIntoConstraints  = false 

     // Adding views to dictionary 
     let viewsDict = [ 
      "myObservationsLabel" : myObservationsLabel, 
      "myObservationsTextView" : myObservationsTextView, 
      "myLabel"    : myLabel, 
      "addButton"    : addButton, 
      "tableView"    : tableView, 
      "myTextField"   : myTextField, 
      "nextStepsLabel"   : nextStepsLabel, 
      "nextStepsTextView"  : nextStepsTextView 
     ] 

     // Setting Constraints to the views in Dictionary 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[myObservationsLabel(25)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[myObservationsLabel]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for myObservationsTextView 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[myObservationsLabel]-10-[myObservationsTextView(100)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[myObservationsTextView]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for nextStepsLabel 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[myObservationsTextView]-10-[nextStepsLabel(25)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[nextStepsLabel]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for nextStepsTextView 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[nextStepsLabel]-10-[nextStepsTextView(100)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[nextStepsTextView]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for myLabel 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[nextStepsTextView]-10-[myLabel(25)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[myLabel]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for addButton 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[nextStepsTextView]-10-[addButton(25)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[addButton]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for myTextField 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[addButton]-10-[myTextField(25)]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[myTextField]-10-|", options: [], metrics: nil, views: viewsDict)) 

     // Setting constraints for Table view 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[myTextField]-10-[tableView]", options: [], metrics: nil, views: viewsDict)) 
     self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[tableView]-10-|", options: [], metrics: nil, views: viewsDict)) 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func save(sender: UIBarButtonItem){ 
     print("Clicked save button") 
    } 

    func cancel(sender: UIBarButtonItem){ 
     print("Clicked cancel button") 
    } 

    func addItems(sender:UIButton!) { 
     print("addItems Button Clicked") 
     self.performSegue(withIdentifier: "showSearchTable", sender: self) 
    } 
    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 

extension ReportVC { 

    func setSelectedDrugMolecules(_ valueSent: [Any]){ 
     print("setSelectedDrugMolecules func called") 
     self.selectedObjectsArray = valueSent 
     print("self.selectedObjectsArray in func: \(self.selectedObjectsArray)") 
     tableView.reloadData() 
    } 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "showSearchTable" { 
      let vc = segue.destination as! DrugMoleculesTVC 
      vc.selectedObjectsArray = self.selectedObjectsArray 
      vc.delegate = self 
     } 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     print("table view loaded in numberOfSections") 
     return 1 
    } 


    // Return the number of rows for each section in your static table 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     print("printing numberOfRowsInSection:\(selectedObjectsArray.count)") 
     return 10 
    } 

    // Return the row for the corresponding section and row 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     print("table view loaded in cellForRowAt") 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) 
     cell.textLabel?.text = "test" 
     return cell 
    } 

    // Customize the section headings for each section 
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     switch(section) { 
     case 0: return "Drug Name" 
     default: fatalError("Unknown section") 
     } 
    } 


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let row  = indexPath.row 
     let section = indexPath.section 

     if section == 0 && row == 0 { 
      //The user has clicked on qualification cell 
      print("TableView Section 0 Row 1 cell has been clicked") 
     } 
     else if section == 0 && row == 1 { 
      // The user has clicked on languages spoken cell 
      print("TableView Section 0 Row 2 cell has been clicked") 
     } 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 10 
    } 

} 
+0

是'selectedObjectsArray'空当调用'numberOfRowsInSection'?和下面的'return 10'是无效的,因为你已经返回了'selectedObjectsArray.count'。 –

+0

你忘了设置委托和数据源吗? tableView.delegate = self和数据源以及? –

+1

@LyjuIEdwinson我把它们设置在'viewDidLoad()' – user44776

回答

0

数据源selectedObjectsArray是空的,当你调用C。当section中的行数为0时,tableView不会调用cellForRowAt

所以,你应该把东西放到selectedObjectsArray呼叫numberOfRowsInSection之前。

或者,当你设置selectedObjectsArray,叫tableView.reloadView

快速的方法,你可以在return 10(fixed number)numberOfRowsInSection看到UI是否工作。

0

,因为我可以从你的get代码得到的,你有datasourceUITableView正确delegateReloadData也在那里。 CGRect.Zero可能是真正的罪魁祸首。由于未定义框架,因此未绘制TableView,因此不调用其数据源方法。

作为一名开发人员,你应该尝试分离的设计和逻辑部分。更喜欢在Storyboard/Xib中免费创建所有视图和约束。它清理代码并使其可以理解。 希望它有帮助。快乐编码!

0

做你试图改变的tableView的帧添加您使用

tableView = `UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)` 

尝试给予一定的帧值到它,而不是CGRect.zero(0,0,0,0),并尝试使用

CGRect(x: 0, y:0, width: self.view.frame.size.width, height: self.view.frame.size.height) 
1

,真正的罪魁祸首是我错过了|,同时设置为我的TableView V:[myTextField]-10-[tableView]|一个垂直约束。

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[myTextField]-10-[tableView]|", options: [], metrics: nil, views: viewsDict)) 

没有你的所有输入,我无法弄清楚,非常感谢!

相关问题