Как сделать UITextField в ячейке таблицы первым респондентом? - PullRequest
4 голосов
/ 05 октября 2011

Если у меня есть серия ячеек таблицы, каждая с текстовым полем, как бы я сделал конкретное текстовое поле стать первым респондентом?

Ответы [ 5 ]

5 голосов
/ 06 октября 2011
// 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;
}

Надеюсь, это поможет вам ...

1 голос
/ 17 декабря 2017

Swift 4 : чтобы сделать текстовое поле первым респондентом в табличном представлении, просто добавьте это расширение в 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
                }
            }
        }
    }
}

Затем в viewDidAppear () вызовите становлениеFirstResponderTextField ():

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

Примечание: Я считаю видимыми ячейки для поиска через

1 голос
/ 05 октября 2011
// 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;
}

Над кодом сделать выделенное (становиться первым) текстовое поле во второй ячейке UItableView. Спасибо

0 голосов
/ 05 октября 2011

UITextField наследуется от UIResponder, который реализует сообщение становитсяFirstResponder.

0 голосов
/ 05 октября 2011

Если вы хотите, чтобы текстовое поле стало первым респондентом после загрузки табличного представления,

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];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...