简体   繁体   English

如何在表格单元格中创建UITextField成为第一响应者?

[英]How to make a UITextField in a table cell become first responder?

如果我有一系列表格单元格,每个表格单元格都有一个文本字段,我将如何使特定的文本字段成为第一个响应者?

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    UITextField *textField = [[UitextField alloc] init];
 ...
     textField.delegate = self;
     textField.tag = indexPath.row;
     [cell addSubView:textField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

i hope this may help you... 我希望这可以帮助你......

Swift 4 :To make textfield the first responder in tableview simply add this extension to tableView: Swift 4 :要使textfield成为tableview中的第一个响应者,只需将此扩展添加到tableView:

extension UITableView {
    func becomeFirstResponderTextField() {
        outer: for cell in visibleCells {
            for view in cell.contentView.subviews {
                if let textfield = view as? UITextField {
                    textfield.becomeFirstResponder()
                    break outer
                }
            }
        }
    }
}

Then in viewDidAppear() call becomeFirstResponderTextField(): 然后在viewDidAppear()中调用becomeFirstResponderTextField():

func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableView.becomeFirstResponderTextField()
}

Note: I am considered visibleCells to search through 注意:我被认为是可以搜索的visibleCells

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}
if(indexPath.row==1)
    {
        [cell.txtField becomeFirstResponder];
    }
return cell;
}

Above code make selected(becomeFirstResponder) textfield in second UItableView cell.Thanks 上面的代码在第二个UItableView单元格中选择(becomeFirstResponder)文本字段。谢谢

If you want the text field to become first responder after loading the table view, 如果希望文本字段在加载表视图后成为第一响应者,

NSIndexPath *indexPath = /* Index path of the cell containg the text field */;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Assuming that you have set the text field's "tag" to 100
UITextField *textField = (UITextField *)[cell viewWithTag:100];
// Make it the first responder
[textField becomeFirstResponder];

UITextField继承自UIResponder,后者实现了becomeFirstResponder消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM