Теги в TextField - PullRequest
       13

Теги в TextField

0 голосов
/ 29 марта 2010

У меня есть три ячейки, внутри которых у меня есть каждое текстовое поле. Теперь я хочу, чтобы пользователь нажимал в каком текстовом поле. Этот метод textFieldDidEndEditing дает мне значение, которое вводит пользователь, но я не получаю тега текстового поля.

Вот мой код:

- (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;
}

Ответы [ 2 ]

1 голос
/ 29 марта 2010

nameField.tag = 1;

agefield.tag = 2;

// в методе делегата просто отметьте

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

} else if (textField.tag ==2) {
 NSLog(@" clicked in Age field");
}
0 голосов
/ 29 марта 2010

Если бы вы сохранили каждое текстовое поле в переменной экземпляра, вы могли бы сделать простое сравнение в своем методе делегата, чтобы увидеть, какое поле завершило редактирование. Вы можете сделать это, даже если вы создадите их полностью динамически, используя ассоциативный массив для сопоставления экземпляра с именем поля или наоборот.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...