views:

1214

answers:

1

I'm basically trying to add a UITextField to a UITableViewCell upon tableView:commitEditingStyle:forRowAtIndexPath:. On insert, I want to add a UITextField over the existing cell.

I setup the textField with something like this:

-(void) setUpIndexField {
    inputField = [[UITextField alloc] init];
    inputField.textAlignment = UITextAlignmentCenter;
    inputField.borderStyle = UITextBorderStyleRoundedRect;    
    inputField.keyboardType = UIKeyboardTypeNamePhonePad;
    inputField.delegate = self;
 }

Then in commitEditingStyle, I have something such as:

- (void)tableView:(UITableView *)tv
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleInsert) {
 if (row == 0) {
  UITableViewCell *cell = [tv cellForRowAtIndexPath:indexPath];
  inputField.frame = [tv cellForRowAtIndexPath:indexPath].frame;
  [cell.contentView addSubview:inputField];
  [inputField becomeFirstResponder];
  return;
 }
// more code etc.
}

When I click on the Insert accessor (+), the textField does indeed get plopped onto the screen, but no where near the cell that's clicking it. It goes off into a random part of the UIView, not the actual cell for the indexPath at where the + was clicked.

Insight on where I went wrong and how I can place the UITextField onto the cell where the + was clicked would be ideal.

+1  A: 

You are setting the frame of inputField to the frame of the cell which will not do what you want. Views inside the cell use the upper left of the frame as 0,0. Your textfield frame should be:

inputField.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);

Since that is relative to it superview that will place the textfield so it it completely covers up the other views in that cell.

Squeegy
Hi Squeegy, that almost is it. The only other problem now is the width of the cell is 320, so it extends past the cell when the style is rounded for UITableView. Is there anyway to have it remain within the bounds of the UITableViewCell and not continue after?
Coocoo4Cocoa
sure. Make the width and height shorter, and increase the origin x and y by a little. Just tweak the values until it fits nice.
Squeegy