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;
}
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];
...
}
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];
It seems that this breaks the selectable cell behavior, and even adding it to [cell contentView]
doesn't fix this. Did I miss something?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…