[英]Setup inputView for different UITextFields in UITableView
我在自定義單元格中有帶有UITextField
的UITableView
。
對於UITextField
我使用自定義inputView
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! Item
cell.price.text = ""
cell.price.delegate = self
return cell
}
func textFieldDidBeginEditing(textField: UITextField) {
textField.inputView = keyboardView
itemPrice = textField
}
func numberWasTapped(number: String!) {
itemPrice.insertText(number!)
}
使用委托方法:
@IBAction func keyTaped(sender: UIButton) {
switch sender.tag {
case 112:
if !isCommaWasPressed {
isCommaWasPressed = true
sender.enabled = false
self.delegate?.numberWasTapped(sender.titleLabel!.text)
}
break
default:
self.delegate?.numberWasTapped(sender.titleLabel!.text)
}
我的自定義inputView
是計算器鍵盤,具有Keyboard類中的所有邏輯:它控制是否按下逗號以及在逗號之后輸入了多少數字。 例如,如果按下了逗號,則Keyboard類中的commaButton.enabled = false
。
當我切換到另一個UITextField
時,我不知道如何在Keyboard類中設置commaButton.enabled = true
。
我在textFieldDidBeginEditing(_:)
方法中嘗試了textField.reloadInputViews()
,但是它不起作用。
我認為,我應該從UITextField
獲取信息,並將其傳遞給Keyboard的類邏輯。 但是我不知道該怎么做。
我找到了解決方案。 我將鍵盤邏輯移到了主要的ViewController上,不需要將textField數據傳遞給Keyboard類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.