尝试浏览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
}
答案 0 :(得分:2)
以下是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;
}
希望它有所帮助。
快乐的编码......
答案 1 :(得分:1)
您应该使用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
}
希望有所帮助。
答案 2 :(得分:0)
对我来说最好的方式。
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
}
}
...