views:

455

answers:

4

Hi All,

Here is the scenario I am working on,

I have a single textField in the cell allocation section and after this section I try to provide give the text field various arguments. Works fine no problem till here, the problem lies how to handle which textField is returning in the delegate method.

My Earlier approach was to simply allocate different text fields for different user input, simple but produces UI glitch when there are many textField(s), hence want avoid it.

for better understanding here is sample code for table delegate method cellAtIndexRow

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellTableIdentifier = @"CellTableIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];

    if (cell == nil) {
        // Create a new cell. CGRectZero allows the cell to determine the appropriate size.

        CGRect cellFrame  = CGRectMake(0,0,300,65);

        cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:CellTableIdentifier] autorelease];

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(8,4,284,25)];
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyDone;
        textField.tag = kTagAddContactTextField;        
        textField.backgroundColor = [UIColor orangeColor];
        [cell.contentView addSubview:textField];
        [textField release];

    }

    UITextField *textField = (UITextField*)[cell.contentView viewWithTag:kTagAddContactTextField];

    switch (indexPath.row) {
        case 0:
            [textField setPlaceholder:@"First Name"];
            break;
        case 1:
            [textField setPlaceholder:@"Last Name"];            
            break;
        case 2:
            [textField setPlaceholder:@"Email"];            
            textField.autocorrectionType = UITextAutocorrectionTypeNo;          
            textField.autocapitalizationType =  UITextAutocapitalizationTypeNone;
            textField.keyboardType = UIKeyboardTypeEmailAddress;
            break;


    }

    //cell.textLabel.text = @"Test";


    return cell;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    //here is the place where I wann to handle various text fields and store there values.

        return YES;

}

any help guys!

A: 

Try pulling

textField.tag = kTagAddContactTextField;

out of the table cell creation code, and assign a different tag value for each row in your UITable.

switch (indexPath.row) {
    case 0:
textField.tag = kFirstNameField;
        [textField setPlaceholder:@"First Name"];
        break;
    case 1:
textField.tag = kLastNameField;
        [textField setPlaceholder:@"Last Name"];            
        break;
    case 2:
textField.tag = kEmail;
        [textField setPlaceholder:@"Email"];            
        textField.autocorrectionType = UITextAutocorrectionTypeNo;          
        textField.autocapitalizationType =  UITextAutocapitalizationTypeNone;
        textField.keyboardType = UIKeyboardTypeEmailAddress;
        break;


}

Then you can distinguish which text field you're getting by the tag returned.

futureelite7
A: 

you can use "tag" property of UIView (which UITextField inherits).

e.g.

 switch (indexPath.row) {
        case 0:
            [textField setPlaceholder:@"First Name"];
            textField.tag=1;//Name
            break;
        case 1:
            [textField setPlaceholder:@"Last Name"]; 
             textField.tag=2;//Surname           
            break;
        case 2:
            [textField setPlaceholder:@"Email"]; 
            textField.tag=3;//Email           
            textField.autocorrectionType = UITextAutocorrectionTypeNo;          
            textField.autocapitalizationType =  UITextAutocapitalizationTypeNone;
            textField.keyboardType = UIKeyboardTypeEmailAddress;
            break;


    }

and in your callback:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

//here is the place where I wann to handle various text fields and store there values.
    switch(textField.tag)
    {
       case 1://Name

            // do stuff;
      break;
         case 2://Surname
           // do stufff;
        break;
        case 3://Email
          //do stuff;
       break;
    }

    return YES;

}

Sanniv
Except then you need to search all three tags to find the textField in order to re-set it to the correct one (in case of cell reuse).
tc.
You can always remove subViews from your cell in case of reusability, but these tags are always fixed from their task.
Sanniv
A: 

Whenever the user chose a textfield for editing didSelectRowAtIndexPath should get called. There you can switch the value an int property currentEdited from which value you can detect what the user was changing.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
   switch(indexPath.row)
   {
      case 0:currentEditing = kEditingFirstName;
      break;

      case 1:....

      ...
   } 

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{

    switch(currentEditing)
    {
       case kEditingFirstName:
            //store first name
            break;

       ....
    }
    return YES;
}

The tagging thing used in the other answers could lead to problems because of the re-use of the textfield.

Kai
That is not always true when u touch inside the text field, didSelectRowAtIndexPath won't be called but the appropriate text field delegate
Ameya
A: 

Several ways of varying ickiness:

  • Use different reuse identifiers and tags for each kind of cell, and switch on the tag. This means cells aren't reused as they might otherwise be, but this is not the end of the world.
  • Follow UIView.superview until you find a subclass of UITableViewCell (textField.superview.superview should work, but this may change in future versions of iPhone OS), and then use [tableView indexPathForRow:cell].
  • Use different reuse identifiers (but the same tag), follow the superview chain until you find a UITableViewCell and look at its reuse identifier.
  • Use a custom UITableViewCell subclass that stores a NSString* or NSInteger key so you know what kind of cell it is (optionally, make the cell the text field's delegate and have the cell notify the view controller of the change).
  • Switch on the placeholder text (very icky when it comes to i18n)
tc.
any snippet code for a situation like me above.
Ameya