我已经几乎完成了一个包含UITextField
的UITableViewCell
的实现。而不是通过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;
}
然后我还实现了didSelectRow
方法,将textField释放以显示其他字段的输入视图。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[amountField resignFirstResponder];
...
}
这段代码能够正常工作,但是当选中表格中的其他行时,整个单元格会被选中并变成蓝色,而我的UITextField所在的那一行并没有被选中,也就是说我可以输入文本但是该行并不被选中。 我已经测试过,发现问题出在以下这一行代码:
[cell addSubview:amountField];
似乎这破坏了可选择单元格的行为,即使将其添加到[cell contentView]
也无法解决。我错过了什么吗?
userInteractionEnabled = NO;
,那么这不会使 UITextField 无法编辑吗? - user814037didSelectRowAtIndexPath:
方法中,在[amountField setUserInteractionEnabled:YES]
行后调用[amountField becomeFirstResponder]
。 - EmptyStack