【发布时间】:2019-11-26 02:56:13
【问题描述】:
我正在使用自定义单元格来显示列表。在我的代码中,我发现 tableview 委托没有被调用。
import UIKit
class MedicineViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var medcineCell: UITableView!
@IBOutlet weak var medInput: UITextField!
var medicine:[String] = ["Medicine"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func medInsert(_ sender: UIButton) {
insertMed()
}
func insertMed(){
medicine.append(medInput.text!)
let indexPath = IndexPath(row: medicine.count - 1, section: 0)
medcineCell.beginUpdates()
medcineCell.insertRows(at: [indexPath], with: .automatic)
medcineCell.endUpdates()
medInput.text = ""
view.endEditing(true)
}
@IBAction func savePresc(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
extension MedicineViewController:UITableViewDataSource,UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return medicine.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let med = medicine[indexPath.row]
let cell = medcineCell.dequeueReusableCell(withIdentifier: "MedicineCell")as! MedicineCell
cell.medTitle.text = med
return cell
}
}
【问题讨论】:
-
首先你没有调用delegate,你甚至没有调用tableviewDataSource的协议来用数据填充tableview
-
你设置了 medcineCell.delegate = self 和 medcineCell.dataSource 吗?