tags:

views:

411

answers:

5

My problem is- i have three cell, within that i have each textfield now i want the user is clicking in which textbox. This method textFieldDidEndEditing gives me the value which user is inputting but i dont get any tag of the textfield

A: 

Hi Mukesh,

nameField.tag = 1;

agefield.tag = 2;

// in the delegate method just check

if (textField.tag == 1) { NSLog(@" clicked in Name field");

} else if (textField.tag ==2) { NSLog(@" clicked in Age field"); }

shreya
hi ..we create on runtime, no.of fields may vary as per the no of object in the array.so please help me getting the tag in the above given method i am not getting the tag assign whether it is 1st text field or 4th text field
mukeshpawar
Hey, Can you explain it in more detail?
shreya
i am dynamically creating the textfield now i have two section in table, i want that under section 1 all the textfield value should get add in one array and another section textfield value in other array for that i want the tag so i can give condition i am not getting how to do that
mukeshpawar
// In the delegate method simply check if(theDelegateTextField == yourTextField){ // do something here}
shreya
hi shreya .. i posted my code below....thnks for help
mukeshpawar
A: 

If you were to retain each text field in an instance variable, you could do a simple comparison in your delegate method to see which field had finished editing. You can do this even if you create them completely dynamically by using an associative array to map from the instance to the field name, or vice-versa.

warrenm
hi ...i didn't get u ..can u elaborate through some example code..
mukeshpawar
A: 

Why are you relaying on textfields?? You will be having the textfields inside a cell right? Which means that you are customizing the UITableViewCell. Tell me how exactly you are adding the textfield into cell?

Manjunath
A: 

hi Manjunath here is my code..thanks in advance


  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { switch (section)  { case 0: return @""; case 1: return @""; default:return @""; } }
  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {      if(optconscodecount != 0 && gotOK == 2) return 2; else return 1; } // Customize the number of rows in the table view.
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch(section)  { case 0: try=1; return conscodecount; case 1: try=2; return optconscodecount; default:return 0;} } // Heights per row
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int section = [indexPath section]; //int row = [indexPath row]; switch (section)  {case 0:return 80.0f; case 1:return 80.0f; default:return 80.0f;} } // Customize the appearance of table view cells.
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     NSLog(@"\n%@", appDelegate.conscodeNameArray);   static NSString *CellIdentifier = @"Cell";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (cell == nil) {         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];     }        // Set up the cell... row = [indexPath row]; section = [indexPath section]; switch (section)  {case 0:try=1;cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"]; if (!cell)  {cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textCell"] autorelease]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)]; [label setText:[appDelegate.conscodeNameArray objectAtIndex:row]];[cell addSubview:label]; [label release]; [cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]]; } UITextField *tf = [[cell subviews] lastObject]; tf.placeholder = [appDelegate.conscodeNameArray objectAtIndex:row]; tf.tag =tagcount; tagcount=tagcount+1; tf.delegate = self; tf.borderStyle = UITextBorderStyleRoundedRect; return cell; break;

    case 1:
        try=2;
        cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        if (!cell) 
        {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textCell"] autorelease];
    
    
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
        [label setText:[appDelegate.optconscodeNameArray objectAtIndex:row]];
        [cell addSubview:label];
        [label release];
        [cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
    }
    UITextField *my = [[cell subviews] lastObject];
    my.tag = 0;
    my.tag = my.tag+1;
    my.placeholder = [appDelegate.optconscodeNameArray objectAtIndex:row];
    my.delegate = self;
    my.borderStyle = UITextBorderStyleRoundedRect;
    return cell;
    break;
    return cell;    
    break;
    
    default: break;

    } return cell; }

mukeshpawar
A: 

hi my code are below.....

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { switch (section)  { case 0: return @""; case 1: return @""; default: return @""; }

}

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {      if(optconscodecount != 0 && gotOK == 2) return 2; else return 1; }

// Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  

switch(section) 
{
    case 0:
        try=1;

        return conscodecount;
    case 1:
        try=2;
        return optconscodecount;
    default:
        return 0;
}

}

// Heights per row - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int section = [indexPath section]; //int row = [indexPath row];

switch (section) 
{
    case 0:
        return 80.0f;
    case 1:
        return 80.0f;
    default:
        return 80.0f;
}

}

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

NSLog(@"\n%@", appDelegate.conscodeNameArray);

    static NSString *CellIdentifier = @"Cell";          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (cell == nil) {         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];     }          // Set up the cell... row = [indexPath row]; section = [indexPath section];

switch (section) 
{

    case 0:
        try=1;
        cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        if (!cell) 
        {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textCell"] autorelease];

            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
            [label setText:[appDelegate.conscodeNameArray objectAtIndex:row]];
            [cell addSubview:label];
            [label release];
            [cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
        }
        UITextField *tf = [[cell subviews] lastObject];
        tf.placeholder = [appDelegate.conscodeNameArray objectAtIndex:row];
        tf.tag =tagcount;
        tagcount=tagcount+1;
        tf.delegate = self;
        tf.borderStyle = UITextBorderStyleRoundedRect;
        return cell;
        break;

    case 1:
        try=2;
        cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        if (!cell) 
        {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textCell"] autorelease];

            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
            [label setText:[appDelegate.optconscodeNameArray objectAtIndex:row]];
            [cell addSubview:label];
            [label release];
            [cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
        }
        UITextField *my = [[cell subviews] lastObject];
        my.tag = 0;
        my.tag = my.tag+1;
        my.placeholder = [appDelegate.optconscodeNameArray objectAtIndex:row];
        my.delegate = self;
        my.borderStyle = UITextBorderStyleRoundedRect;
        return cell;
        break;


        return cell;    
        break;
    default:
        break;
}

return cell;

}

mukeshpawar
Ok I saw your code. Instead of adding textfield and label in delegate methods, you create a customized uitableviewcell class and there add these fields. So that you need not to keep trak of all text fields you added. and even by using acessors you can set or get the textfield value whenever you want.
Manjunath
Now when you select a Cell you will get reference to textfield and label of that cell and life becomes easier :)
Manjunath