简体   繁体   English

在iOS中选择单元格时,需要在UITableView行中启用UITextField

[英]Need to enable UITextField in row of UITableView when cell is selected in iOS

I have created a custom cell that places a UITextField in each row of a UITableView. 我创建了一个自定义单元格,在UITableView的每一行中放置一个UITextField。 What I would like to do is enable the UITextField for a particular row (ie activate the cursor in that textfield), when the user presses on anywhere inside the cell. 当用户按下单元格内的任何位置时,我想要做的是为特定行启用UITextField(即激活该文本字段中的光标)。

I have the following method where I try to do this: 我尝试这样做有以下方法:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     [_cell.textField setUserInteractionEnabled:TRUE];

}

However, this is not working. 但是,这不起作用。 Can anyone see what I'm doing wrong? 谁能看到我做错了什么?

Try this code: 试试这段代码:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     [_cell.textField becomeFirstResponder];
}

You're on the right track placing this inside didSelectRowAtIndexPath:. 你在正确的轨道上把它放在didSelectRowAtIndexPath:里面。 However, setting user interaction enabled on the textfield just means that the user would be allowed to interact with the object if they tried. 但是,在文本字段上设置用户交互仅意味着允许用户在尝试时与对象进行交互。 You're looking for becomeFirstResponder. 您正在寻找becomeFirstResponder。

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.textField becomeFirstResponder];
}

What is this _cell iVar? 这个_cell iVar是什么? Are you setting it somewhere? 你在某处设置它吗?

I think what you are trying to accomplish is this: 我想你想要完成的是:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     [cell.textField becomeFirstResponder];
}

Notice that in this way you are getting the cell for the tapped indexPath. 请注意,通过这种方式,您将获取tapped indexPath的单元格。

And also, just for information, it's a pattern in objective-C to use booleans as YES or NO , instead of true or false . 而且,仅仅是为了获取信息,它是Objective-C中使用布尔true YESNO而不是truefalse

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

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