在Static UITableView上使用UITextfields的Previous和Next键盘按钮

时间:2015-10-29 14:27:27

标签: ios iphone swift uitableview

我正在使用带有静态单元格的UITableView来显示包含多个UITextFields的表单(每个表单都有一个从9000到9015的唯一有序标记)。 当我选择UITextField时,键盘会显示UIToolbar,其中有2个按钮,上一个和下一个。只要在屏幕上绘制上一个或下一个UITextField,按钮就可以正常工作,否则我无法选择它,因为viewWithTag无法找到该字段。

DEMO:http://s15.postimg.org/5xjsoiupl/ezgif_com_video_to_gif.gif

编辑:我尝试使用IQKeyboardManager,但它有相同的错误。如果单元格不可见,则不会检测到它们,因此禁用下一个或上一个箭头...

UITextFields

let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
numberToolbar.barStyle = UIBarStyle.Default
numberToolbar.tintColor = color_green

prev_keyboard_button = UIBarButtonItem(title: "Previous", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardPrevButtonTapped:")
next_keyboard_button = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardNextButtonTapped:")

numberToolbar.items = [
    prev_keyboard_button,
    next_keyboard_button,
    UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
    UIBarButtonItem(title: "OK", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardOKButtonTapped:")]

    numberToolbar.sizeToFit()
// bind vars
var_input_name.inputAccessoryView = numberToolbar
var_input_name.delegate = self
var_input_name.tag = 9000  // 9000 + index (0,1,2,3,..)
...

上一个和下一个代码:

func textFieldDidBeginEditing(textField: UITextField) {

    // current tag
    current_input_tag = textField.tag

    // check if it is the first
    prev_keyboard_button.enabled = !(textField.tag == 9000)

    // check if it is the last
    next_keyboard_button.enabled = !(textField.tag == 9015)

}

func keyboardPrevButtonTapped(sender: UIBarButtonItem) {

    if current_input_tag > 9000 {
        // find next input
        if let input = self.view.viewWithTag(current_input_tag - 1) as? UITextField {
            input.becomeFirstResponder()
        }
    }

}

func keyboardNextButtonTapped(sender: UIBarButtonItem) {

    if current_input_tag < 9015 {
        // find next input
        if let input = self.view.viewWithTag(current_input_tag + 1) as? UITextField {
            input.becomeFirstResponder()
        }
    }

}

有没有办法总是绘制静态单元格,还是我应该以不同方式实现它?

1 个答案:

答案 0 :(得分:0)

如果卖出不在屏幕上,它将被释放。无论您使用静态还是动态细胞,都可以。可能你现在试图查找不存在的视图