UITableViewCell与UITextField失去选择UITableView行的能力? [英] UITableViewCell with UITextField losing the ability to select UITableView row?

查看:98
本文介绍了UITableViewCell与UITextField失去选择UITableView行的能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完成了实现一个 UITableViewCell UITextField 。而是通过 CGRectMake UITableViewCell.contentView 我实现了更简单的方法:

I am almost done implementing a UITableViewCell with a UITextField in it. Rather then going through CGRectMake and UITableViewCell.contentView I have implemented it the simpler way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
    amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)];
    amountField.placeholder = @"Enter amount";
    amountField.keyboardType = UIKeyboardTypeDecimalPad;
    amountField.textAlignment = UITextAlignmentRight;
    amountField.clearButtonMode = UITextFieldViewModeNever; 
    [amountField setDelegate:self];

    [[cell textLabel] setText:@"Amount"];
    [cell addSubview:amountField];
    return cell;
}

然后我也实现了 didSelectRow 方法,退出textField以允许显示其他字段输入视图。

And then I also implemented the didSelectRow method, resigning the textField to allow showing the other fields input views.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    [amountField resignFirstResponder];
    ...
}

表中的其他行,当那些其他被选择时,整个单元格被选择并变为蓝色,而与我的UITextField的一个不是,我的意思是字段被选中,我可以输入文本,但单元格没有选择。
我测试了它,发现问题是在行:

This works smoothly, only thing is there are other rows in the table, when those others are selected the entire cell is selected and turns Blue, while the one with my UITextField doesn't, I mean the field is selected and I can enter text but the cell is not selected. I have tested it and figured out the problem is in the line:

[cell addSubview:amountField];

似乎这打破了可选单元格行为,甚至将其添加到 [cell contentView] 无法解决此问题。

It seems that this breaks the selectable cell behavior, and even adding it to [cell contentView] doesn't fix this. Did I miss something?

推荐答案

如果文本字段的 userInteractionEnabled 设置为,它填充整个单元格,你不能让单元听到触摸。要使单元格响应触摸,您需要将文本字段的 userInteractionEnabled 设置为 NO

If the text field has userInteractionEnabled set to YES, and it fills the entire cell, you can not get the cell to listen to touch. In order to get the cell to respond to touches, you need to set the userInteractionEnabled of the text field to NO.

修改:如果您要使文本字段可编辑,则在选择单元格时,请在 didSelectRowAtIndexPath:方法中添加以下代码:

And if you want to make the text field editable, when the cell is selected, add the following code in didSelectRowAtIndexPath: method,

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

    // get the reference to the text field
    [textField setUserInteractionEnabled:YES];
    [textField becomeFirstResponder];
}

这篇关于UITableViewCell与UITextField失去选择UITableView行的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆