I am almost done implementing a UITableViewCell
with a UITextField
in it. Rather then going through CGRectMake
and UITableViewCe
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:
- (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
- (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;
}