从 TableViewController 获取 CustomCell 上的 UITextField 的委托触发器和普通触发器之间有什么区别?
Whats difference between delegate trigger and normal trigger to get UITextField on CustomCell from TableViewController?
UITableViewCell 上有两个 UITextField 实例。
我想在 CustomCell 上获取 UITextField 的文本。
该函数是 test()。
模式 A 是 UITextField 的代表,
模式B是按格功能。
我测试了模式 B 有效,但模式 A 无效。
我其实想让A工作,
这是我的问题,
为什么模式 A 不起作用?
还有,如何从 TableViewController 获取 CustomCell 上 UITextField 的文本?
//Only needed code is written.
protocol CustomTableViewCellDelegate{
func getInputed(textField: UITextField)
}
class CustomTableViewCell: UITableViewCell , UITextFieldDelegate{
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
if myTextField != nil {
myTextField.delegate = self
myTextField2.delegate = self
}
}
//the session is to end
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
if delegate != nil {
self.delegate?.getInputed(textField)
}
return true
}
}
class TableViewController: UITableViewController, CustomTableViewCellDelegate{
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Im sure I write like this
cell.delegate = self
}
//Pattern A
func getInputed(textField: UITextField){
test()
}
//Pattern B
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
test()
}
func test(){
self.myTableView.reloadData()
let cell = myTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! CustomTableViewCell
print(cell.myTextField.text)
print(cell.myTextField.tag)
print(cell.myTextField2.text)
print(cell.myTextField2.tag)
}
}
根据我们在 comments/chat 中的讨论,我添加了答案。
按以下方式修改代码有效:
protocol CustomTableViewCellDelegate{
func getInputed(cell:CustomTableViewCell)
}
//the session is to end
func textFieldDidEndEditing(cell:CustomTableViewCell) {
if delegate != nil {
self.delegate?.getInputed(self)
}
}
func test(cell:CustomTableViewCell) {
print(cell.myTextField.text)
print(cell.myTextField.tag)
print(cell.myTextField2.text)
print(cell.myTextField2.tag)
self.myTableView.reloadData()
}
UITableViewCell 上有两个 UITextField 实例。 我想在 CustomCell 上获取 UITextField 的文本。 该函数是 test()。
模式 A 是 UITextField 的代表, 模式B是按格功能。
我测试了模式 B 有效,但模式 A 无效。 我其实想让A工作, 这是我的问题, 为什么模式 A 不起作用?
还有,如何从 TableViewController 获取 CustomCell 上 UITextField 的文本?
//Only needed code is written.
protocol CustomTableViewCellDelegate{
func getInputed(textField: UITextField)
}
class CustomTableViewCell: UITableViewCell , UITextFieldDelegate{
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
if myTextField != nil {
myTextField.delegate = self
myTextField2.delegate = self
}
}
//the session is to end
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
if delegate != nil {
self.delegate?.getInputed(textField)
}
return true
}
}
class TableViewController: UITableViewController, CustomTableViewCellDelegate{
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Im sure I write like this
cell.delegate = self
}
//Pattern A
func getInputed(textField: UITextField){
test()
}
//Pattern B
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
test()
}
func test(){
self.myTableView.reloadData()
let cell = myTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! CustomTableViewCell
print(cell.myTextField.text)
print(cell.myTextField.tag)
print(cell.myTextField2.text)
print(cell.myTextField2.tag)
}
}
根据我们在 comments/chat 中的讨论,我添加了答案。 按以下方式修改代码有效:
protocol CustomTableViewCellDelegate{
func getInputed(cell:CustomTableViewCell)
}
//the session is to end
func textFieldDidEndEditing(cell:CustomTableViewCell) {
if delegate != nil {
self.delegate?.getInputed(self)
}
}
func test(cell:CustomTableViewCell) {
print(cell.myTextField.text)
print(cell.myTextField.tag)
print(cell.myTextField2.text)
print(cell.myTextField2.tag)
self.myTableView.reloadData()
}