【问题标题】:Navigate through textfields in UITableView swift快速浏览 UITableView 中的文本字段
【发布时间】:2018-10-11 02:07:49
【问题描述】:

尝试浏览 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
}

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    这是 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;
    }
    

    希望对你有帮助。

    编码愉快...

    【讨论】:

    • 它没有调用文本字段的代表,实现了delegate = self并在上面也采用了UITextFieldDelegate
    • 您是否在视图控制器中包含了 UITextFieldDelegate?看到我们在 cellForRowAtIndex 中设置了委托。所以绝对应该打电话。我已经检查过了。只需检查那里,让我知道
    • 没问题,我的错误委托将在自定义单元类中工作。
    • 但上面没有定义如何导航到下一个文本字段
    • 点击下一步返回
    【解决方案2】:

    您应该使用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
        }
    

    希望有所帮助。

    【讨论】:

    • 你能发个 swift 2
    • 加油!不要懒得将其更改为 Swift 2 :) 您所要做的就是更改 tableView 数据源方法名称,这将解决您的问题。
    【解决方案3】:

    对我来说最好的方式。

    UITableViewCell

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

    UITableViewDataSource、UITableViewDelegate

    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
    }
    

    UITextFieldDelegate

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-01
      • 2015-09-04
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多