我有一个TableView,其中加载了自定义单元格。 在自定义单元格中有一个文本字段,在填充表格视图时会自动加载一个文本框,以关闭自定义拨号盘。 此浮动数字键盘在初始化期间已传递textfield元素。但是只要我按下数字键盘上的按钮,文本字段就不会更改(这仅在表视图单元格中发生)。
所以回顾一下:
UICellView->内部-> UITextField UICellView由其UITableView加载时,将初始化CustomDialPad(UIView) 在CustomDialPad初始化到其类的方法的过程中,将传递UITextField
当我按下CustomDialPad中的按钮之一时,UITextField不会自行修改。
所以我在3个不同阶段打印了UITextField元素的转储。
第一阶段结果-单元格加载时转储UITextField:
▿ Optional(<UITextField: 0x7fa83c0cd200; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x60c000229ae0>>)
- some: <UITextField: 0x7fa83c0cd200; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x60c000229ae0>> #0
- super: UIControl
- super: UIView
- super: UIResponder
- super: NSObject
第2阶段结果-加载CustomDialPad时转储UITextField:
- <UITextField: 0x7fa83c0cd200; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60c00005d190>; layer = <CALayer: 0x60c000229ae0>> #0
- super: UIControl
- super: UIView
- super: UIResponder
- super: NSObject
▿ Optional(<UITextField: 0x7fa83c096a00; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x60c00022b8a0>>)
- some: <UITextField: 0x7fa83c096a00; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x60c00022b8a0>> #0
- super: UIControl
- super: UIView
- super: UIResponder
- super: NSObject
第3阶段结果-按下CUstomDialPad的按钮时转储UITextField
▿ Optional(<UITextField: 0x7fa83c0cd200; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60c00005d190>; layer = <CALayer: 0x60c000229ae0>>)
- some: <UITextField: 0x7fa83c0cd200; frame = (358 6; 104 30); text = '103.12'; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60c00005d190>; layer = <CALayer: 0x60c000229ae0>> #0
- super: UIControl
- super: UIView
- super: UIResponder
- super: NSObject
您可以看到这里是3个不同阶段的参考:
Phase 1: Optional(UITextField: 0x7fa83c0cd200)
Phase 2: UITextField: 0x7fa83c0cd200
Optional(UITextField: 0x7fa83c096a00)
Phase 3: Optional(UITextField: 0x7fa83c0cd200)
为什么在第2阶段有2个不同的引用? 难道是在CustomDialPad按下按钮期间UITextFields中的值不改变的原因吗? 在第2阶段的其他阶段中如何检索具有相同引用的元素?
关于, Matteo
帮助代码:
自定义UIView类NumbersPadView
class NumbersPadView: UIView {
@IBOutlet weak var number1: UIButton!
@IBOutlet weak var number2: UIButton!
@IBOutlet weak var number3: UIButton!
@IBOutlet weak var number4: UIButton!
@IBOutlet weak var number5: UIButton!
@IBOutlet weak var number6: UIButton!
@IBOutlet weak var number7: UIButton!
@IBOutlet weak var number8: UIButton!
@IBOutlet weak var number9: UIButton!
@IBOutlet weak var zero: UIButton!
@IBOutlet weak var dot: UIButton!
@IBOutlet weak var delete: UIButton!
@IBOutlet weak var done: UIButton!
var connectedTextField: UITextField!
var isFirstInput = true
override func draw(_ rect: CGRect) {
// Drawing code
}
func showMyself(showing: UIView, element: UITextField) {
self.connectedTextField = element
self.connectedTextField.resignFirstResponder()
self.isFirstInput = true
self.connectedTextField.selectedTextRange = self.connectedTextField.textRange(from: self.connectedTextField.beginningOfDocument, to: self.connectedTextField.endOfDocument)
self.alpha = 0.0
let frame = showing.convert(self.connectedTextField.frame, from:self.connectedTextField.superview)
self.frame.origin.x = CGFloat(Double(frame.origin.x) - Double(self.frame.width) - 2)
self.frame.origin.y = CGFloat((Double(frame.origin.y) + Double(element.frame.height) / 2) - Double(self.frame.height) / 2)
if self.superview == nil {
showing.addSubview(self)
}
UIView.animate(withDuration: 0.4, animations: {
self.alpha = 1.0
})
dump(self.connectedTextField) //Phase 2
}
@IBAction func buttonPressed(_ sender: UIButton) {
if (isFirstInput) {
self.connectedTextField.text = ""
}
self.isFirstInput = false
switch sender.tag {
case 10: //zero
self.connectedTextField.text = self.connectedTextField.text! + "0"
break
case 12: //back
if self.connectedTextField.text != "" {
var tmpText = self.connectedTextField.text
tmpText = tmpText?.substr(start: 1, length: (tmpText?.length)! - 1)
self.connectedTextField.text = tmpText
if tmpText == "." {
tmpText = tmpText?.substr(start: 1, length: (tmpText?.length)! - 1)
self.connectedTextField.text = tmpText
}
}
break
case 13:
if self.connectedTextField.text != "" && self.connectedTextField.text?.range(of:".") == nil {
self.connectedTextField.text = self.connectedTextField.text! + "."
}
break
default:
self.connectedTextField.text = self.connectedTextField.text! + String(sender.tag)
break
}
self.connectedTextField.selectedTextRange = self.connectedTextField.textRange(from: self.connectedTextField.endOfDocument, to: self.connectedTextField.endOfDocument)
dump(self.connectedTextField) //Phase 3
}
@IBAction func donePressed(_ sender: UIButton) {
let tmpDoubleValue = self.connectedTextField.text?.getDouble()
self.connectedTextField.text = tmpDoubleValue?.toString(2)
self.connectedTextField.selectedTextRange = self.connectedTextField.textRange(from: self.connectedTextField.endOfDocument, to: self.connectedTextField.endOfDocument)
UIView.animate(withDuration: 0.3, animations: {
self.alpha = 0.0
})
self.connectedTextField.resignFirstResponder()
}
填充UITableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tmpCell = Bundle.main.loadNibNamed("ItemCellNew", owner: self, options: nil)?.first as! ItemCellNew
tmpCell.setCell(item: items[indexPath.row])
tmpCell.POSItemsFunctionsDelegate = self
if self.idOrder != "" {
tmpCell.setOrderCell()
}
dump(tmpCell.amountInput) // Phase 1
return tmpCell
}
Cell方法setCell()
func setCell(item: itemStruct) {
if item.realQty < item.qty {
self.bkgView?.backgroundColor = UIColor(named: "NO_QUANTITY")
}
self.title.text = item.title
self.brand.text = item.brand
self.qty.text = String(item.qty)
self.amountInput.text = String(item.prc)
self.subTotal.text = String(item.prc * Double(item.qty))
self.amountInput.setShowingNumberPad()
}
UITextField的扩展名:
func setShowingNumberPad() {
self.addTarget(self, action: #selector(didEditingBegin(_:)), for: .editingDidBegin)
}
@objc func didEditingBegin(_ sender:UITextField) {
var customKeyboard: NumbersPadView!
let views = sender.parentViewController?.view.subviews.filter{$0 is NumbersPadView}
if views?.count == 0 {
customKeyboard = (Bundle.main.loadNibNamed("NumbersPadView", owner: self, options: nil)?.first as? NumbersPadView)!
} else {
customKeyboard = views?.first as! NumbersPadView
}
customKeyboard.showMyself(showing: (self.parentViewController?.view)!, element: sender as UITextField)
}
答案 0 :(得分:0)
我找到了解决此问题的方法,方法是修改包含在与选定单元格的indexPath相对应的items数组中的项目,然后将数据重新加载到tableview内部。
您无法直接访问位于单元格内部的输入字段。