【发布时间】:2016-10-15 21:55:16
【问题描述】:
我有一个子类CustomCell,它继承自我的父类CreateEvent。子类描述了表格视图单元格的各个单元格,它位于CreateEvent 视图控制器上。在一个特定的单元格中,我有一个文本字段,它链接到 CustomCell 文件,但是当用户输入文本字段时,我无法从该文本字段中获取值。我也无法通过外部触摸关闭键盘并按返回键,但我主要专注于从文本字段中获取文本。我熟悉在一个普通的 swift 文件上执行这些功能,但因为这是一个子类和两个 swift 文件,我不知道该怎么做。这是我的代码:
class CustomCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var entranceFeeTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
和
class CreateEventVC: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate, UITextFieldDelegate {
override func viewDidLoad() {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String, for: indexPath) as! CustomCell
// the following code describes the array for an expandable drop down
if currentCellDescriptor["cellIdentifier"] as! String == "idCellNormal" {
if let primaryTitle = currentCellDescriptor["primaryTitle"] {
cell.textLabel?.text = primaryTitle as? String
}
eventType = ((cellDescriptors[0] as! NSMutableArray)[0] as! NSDictionary)["primaryTitle"]! as! String
if let secondaryTitle = currentCellDescriptor["secondaryTitle"] {
cell.detailTextLabel?.text = secondaryTitle as? String
}
}
else if currentCellDescriptor["cellIdentifier"] as! String == "idCellTextfield" {
cell.textField.placeholder = currentCellDescriptor["primaryTitle"] as? String
}
else if currentCellDescriptor["cellIdentifier"] as! String == "idCellValuePicker" {
cell.textLabel?.text = currentCellDescriptor["primaryTitle"] as? String
}
cell.delegate = self
return cell
}
func textFieldShouldEndEditing(textField:UITextField) -> Bool {
entranceFeeAmount = cell.entranceFeeTextField.text!
return true;
}
}
我的主要问题是我不确定如何设置文本字段委托,以及如何正确符合委托。
【问题讨论】:
-
您是在情节提要/NIB 中创建单元格吗?
-
我正在创建一个包含文本字段的 .xib 文件中使用的特定单元格
-
您会只有一个这样的单元格,还是打算拥有多个?
标签: swift uitableview delegates textfield