在Swift中浏览文本字段

vxbzzdmp  于 5个月前  发布在  Swift
关注(0)|答案(3)|浏览(76)

尝试在UITableView中导航文本字段。能够在简单视图中导航文本字段,但无法在UITableView中导航文本字段。
下面是到目前为止尝试的代码:

func textFieldDidBeginEditing(textField: UITextField)
{

    //textField.inputAccessoryView = numberToolbar
    if(textField == txtConfirmPassword)
    {
        textField.returnKeyType = UIReturnKeyType.Done
    }
    else
    {
        textField.returnKeyType = UIReturnKeyType.Next
    }
}

字符串
请指导,谢谢。
更新:

func textFieldDidBeginEditing(textField: UITextField)
{
    if(delegate != nil)
    {
        delegate?.performSelector(NSSelectorFromString("editingTextField"))
    }

    if(textField.tag == 3)
    {
        textField.returnKeyType = UIReturnKeyType.Done
    }
    else
    {
        textField.returnKeyType = UIReturnKeyType.Next
    }
}

func textFieldShouldReturn(textField: UITextField) -> Bool
{
    if(delegate != nil)
    {
        delegate?.performSelector(NSSelectorFromString("editingDone"))
    }
    if(textField.tag == 3)
    {
        textField.resignFirstResponder()
        delegate?.performSelector(NSSelectorFromString("editingDone"))

    }

     let nextTage=textField.tag+1
    // Try to find next responder
    let nextResponder=textField.superview?.viewWithTag(nextTage) as UIResponder!

    if (nextResponder != nil){
        // Found next responder, so set it.
        nextResponder?.becomeFirstResponder()
    }
    else
    {
        // Not found, so remove keyboard
        textField.resignFirstResponder()
    }
    return true
}

inb24sb2

inb24sb21#

下面是Swift 2.1的代码
在这里,我使用了SampleTableViewCell,这是一个自定义单元格,我在其中创建了textField。
出于这个目的,我刚刚采取了基于单元格的标记,因此在textField的委托方法中访问它时,您可以知道textField是哪个单元格。

验证码如下:

//UITableView Delegate

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SampleTableViewCell
    cell.txtFld.delegate = self;
    cell.txtFld.tag = indexPath.row + 100
        
    cell.selectionStyle = UITableViewCellSelectionStyle.Gray;
    
    
    return cell;
}

字符串
更新:这是TextField的委托

//UITextField Delegate

func textFieldDidBeginEditing(textField: UITextField)
{
    
    //textField.inputAccessoryView = numberToolbar
    if(textField.tag == 100)
    {
        textField.returnKeyType = UIReturnKeyType.Done
    }
    else
    {
        textField.returnKeyType = UIReturnKeyType.Next
    }
}


在这里,我有导航到下一个文本字段通过单击返回按钮的文本字段。

// called when 'return' key pressed. return NO to ignore.
func textFieldShouldReturn(textField: UITextField) -> Bool
{
    textField.resignFirstResponder();
    
    if(textField.tag == 100)
    {
        let txtFld = self.tblList.viewWithTag(101);
        txtFld?.becomeFirstResponder();
    }
    
    return true;
}

hjqgdpho

hjqgdpho2#

您应该使用tag来检查当前textfield是否是第一响应者。

  • 注意:Swift 3.*
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // let's say that you are reading the date from an array...
        return myArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

        // here you set the tag
        cell.myTextfield?.tag = indexPath.row

        // ...

        return cell
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        // now, based on textField you can check (tag == myArray.count - 1 means it is the last)
        textField.returnKeyType = (textField.tag == myArray.count - 1) ? .done : .next
    }

字符串
希望这对你有帮助。

db2dz4w8

db2dz4w83#

对我来说最好的方式。

可扩展视图单元

class CustomTableViewCell: UITableViewCell {
     var nextTextField: (() -> Void)?
}

字符串

可扩展视图数据源,可扩展视图委托

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    cell.nextTextField = { in
        guard let cell = tableView.cellForRow(at: IndexPath(row: indexPath.row + 1, section: 0)) as? CustomTableViewCell else {
            return
        }
        cell.textField.becomeFirstResponder()
    }
    return cell
}

扩展字段代表

...
func textFieldDidBeginEditing(textfield: RCTextField) {
    if viewModel.items.count-1 == textfield.tag {
       textField.returnKeyType = UIReturnKeyType.done
    } else {
       textField.returnKeyType = UIReturnKeyType.next
    }
}
...

相关问题