ios 带有 UITextField 的 UITableViewCell 失去了选择 UITableView 行的能力?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6579904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableViewCell with UITextField losing the ability to select UITableView row?
提问by Fabrizio Prosperi
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
了UITextField
在其中实现了a 。而不是通过CGRectMake
,UITableViewCell.contentView
我以更简单的方式实现了它:
- (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;
}
And then I also implemented the didSelectRow
method, resigning the textField to allow showing the other fields input views.
然后我还实现了该didSelectRow
方法,放弃了 textField 以允许显示其他字段的输入视图。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[amountField resignFirstResponder];
...
}
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:
这是顺利的工作,只有表中只有其他行,当其他人被选择时,选择整个小区并转动蓝色,而一个带有我的UITextfield的那个没有,我的意思是选择该字段,我可以输入文本,我可以输入文本但未选择单元格。我已经对其进行了测试,并发现问题出在该行中:
[cell addSubview:amountField];
It seems that this breaks the selectable cell behavior, and even adding it to [cell contentView]
doesn't fix this. Did I miss something?
这似乎破坏了可选择的单元格行为,甚至将其添加到[cell contentView]
也无法解决此问题。我错过了什么?
回答by EmptyStack
If the text field has userInteractionEnabledset 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 userInteractionEnabledof the text field to NO.
如果文本字段将userInteractionEnabled设置为YES,并且它填充了整个单元格,则您无法让单元格听触摸。为了让单元格响应触摸,您需要将文本字段的userInteractionEnabled设置为NO。
Edit:And if you want to make the text field editable, when the cell is selected, add the following code in didSelectRowAtIndexPath:method,
编辑:如果要使文本字段可编辑,则在选中单元格时,在didSelectRowAtIndexPath:方法中添加以下代码,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// get the reference to the text field
[textField setUserInteractionEnabled:YES];
[textField becomeFirstResponder];
}
回答by Mic Pringle
You've not brokenanything by adding a subview, instead the UITextField is capturing the touch ahead of the UITableViewCell. You can test this by tapping outside of the UITextField but within the bounds of the UITableViewCell and you'll see it does in fact still select as you would expect.
添加子视图并没有破坏任何东西,而是 UITextField 捕获了 UITableViewCell 之前的触摸。您可以通过在 UITextField 之外但在 UITableViewCell 的范围内点击来测试这一点,您会看到它实际上仍然按照您的预期进行选择。
To get round this, you could subclass UITextField and add a UITableView property. Set the property when you instantiate the UITextField and add it to the cell.
为了解决这个问题,你可以继承 UITextField 并添加一个 UITableView 属性。在实例化 UITextField 并将其添加到单元格时设置该属性。
amountField.tableView = tableView;
Then you'd need to override becomeFirstResponderin your subclass, and in the method get the row for the cell with the UITextField and then select it manually
然后你需要在你的子类中覆盖becomeFirstResponder,并在方法中使用 UITextField 获取单元格的行,然后手动选择它
- (BOOL)becomeFirstResponder
{
// Get the rect of the UITextView in the UITableView's coordinate system
CGRect position = [self convertRect:self.frame toView:self.tableView];
// Ask the UITableView for all the rows in that rect, in this case it should be 1
NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:position];
// Then manually select it
[self.tableView selectRowAtIndexPath:[indexPaths objectAtIndex:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
return YES;
}
回答by iPrabu
First Thing is you havent used reusable cells. The coding which you provided will cause a lot of memory.
第一件事是你没有使用可重复使用的单元格。您提供的编码将导致大量内存。
Next thing is you can select the row by touching the area other than the textfield.
接下来是您可以通过触摸文本字段以外的区域来选择行。
One solution for your ques is
您的问题的一种解决方案是
In your textField Delegate
在您的 textField 委托中
UITableViewCell *cell = (UITableViewCell *)[textField superView];
cell.selected=YES; //I think this will call the didSelectRowATIndex;
I am not sure this will work. But worth a try.
我不确定这会起作用。但值得一试。
回答by iMOBDEV
First of all you needed to use reuseIdentifier for cellForRowAtIndexPath, reason if you do not use reuseIdentifier is: when you scroll up and down, it will always allocate new cell and new textfields so you need to put cell==nil condition, so revised code is here:
首先你需要为cellForRowAtIndexPath使用reuseIdentifier,如果你不使用reuseIdentifier的原因是:当你向上和向下滚动时,它总是会分配新的单元格和新的文本字段,所以你需要把cell==nil条件,所以修改了代码在这儿:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell==nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
UITextField *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.contentView addSubview:amountField];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setText:@"Amount"];
return cell;
}
In didSelect delegate method you can do like this
在 didSelect 委托方法中,您可以这样做
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[prevField resignFirstResponder];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *view = [[cell.contentView subviews] lastObject];
if([view isKindOfClass:[UITextField class]]{
currentField = (UITextField*)view;
}
[currentField becomeFirstResponder];
prevField = currentField;
}
回答by Mutablegopi
inside the cell for row at index path add this line which is given in bold,
在索引路径行的单元格内添加以粗体给出的这一行,
- (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"];
**for (UIView *cellSubViews in cell.subviews) {
cellSubViews.userInteractionEnabled = NO;
}**
[cell addSubview:amountField];
return cell;
}
try this, it will work
试试这个,它会起作用