我已经在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
}
}
是'selectedObjectsArray'空当调用'numberOfRowsInSection'?和下面的'return 10'是无效的,因为你已经返回了'selectedObjectsArray.count'。 –
你忘了设置委托和数据源吗? tableView.delegate = self和数据源以及? –
@LyjuIEdwinson我把它们设置在'viewDidLoad()' – user44776