Первый респондент не установлен в настраиваемом TableView с настраиваемым текстовым полем

У меня есть таблица, содержащая кнопки и текстовые поля, я создаю новую ячейку, когда я нажимаю, текстовое поле или строку

Но когда у меня более одной строки, он не назначает первого респондента на текстовое поле, когда я нажимаю на текстовое поле, чтобы напрямую перейти к другому полю.

вот мой код для cellForRowAtIndexPath и textFieldDidEndEditing

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
        {
            cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];



            wholeSale = [[UITextField alloc] initWithFrame:CGRectMake(240, 0, 100, 32)];
            wholeSale.borderStyle = UITextBorderStyleLine;
            wholeSale.layer.borderWidth=2.0f;
            wholeSale.layer.borderColor=[UIColor colorWithRed:81/255.0  green:105/255.0  blue:169/255.0  alpha:(1)].CGColor;
            wholeSale.clipsToBounds=YES;
            [wholeSale setFont:[UIFont boldSystemFontOfSize:14]];
            [wholeSale setTextAlignment:UITextAlignmentCenter];
            [wholeSale setBackgroundColor:[UIColor whiteColor]];
            wholeSale.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
            wholeSale.placeholder=@"Money";
            [wholeSale setDelegate:self];
            wholeSale.tag=102;
            [cell.contentView addSubview:wholeSale];

            cost = [[UITextField alloc] initWithFrame:CGRectMake(338, 0, 100, 32)];
            cost.borderStyle = UITextBorderStyleLine;
            cost.layer.borderWidth=2.0f;
            cost.layer.borderColor=[UIColor colorWithRed:81/255.0  green:105/255.0  blue:169/255.0  alpha:(1)].CGColor;
            cost.clipsToBounds=YES;
            [cost setFont:[UIFont boldSystemFontOfSize:14]];
            [cost setTextAlignment:UITextAlignmentCenter];
            [cost setBackgroundColor:[UIColor whiteColor]];
            cost.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
            cost.placeholder=@"Cost";
            [cost setDelegate:self];
            cost.tag=103;
            [cell.contentView addSubview:cost];



        }

        wholeSale=(UITextField*)[cell.contentView viewWithTag:102];
        cost=(UITextField*)[cell.contentView viewWithTag:103];  
}



-(void)textFieldDidEndEditing:(UITextField *)textField
{
    //    [textField resignFirstResponder];

    UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    NSLog(@"%d",textFieldIndexPath.row);

    indexPathRow=textFieldIndexPath.row;



    Products *record;

    if (indexPathRow==[allProdutsData count]) {
        NSManagedObjectContext *context = [appDelegate managedObjectContext]; // get AppDelegate's NSManagedObjectContext
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Products" inManagedObjectContext:context];
        record = [[Products alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
        NSLog(@"id==%d",[allProdutsData count]);
        Products *lastFile=[allProdutsData lastObject];
        NSLog(@"%@",[allProdutsData lastObject]);
        [record setP_ID:[NSString stringWithFormat:@"%@",lastFile.p_ID]];
         NSLog(@"%@",record);
    }
    else
    {
        NSLog(@"%d",indexPathRow);
        //            NSLog(@"%@",[allFilesData objectAtIndex:filesDateIndex]);
        record=[allProdutsData objectAtIndex:indexPathRow];
        NSLog(@"%@",record);
    }

    iif (textField.tag==102)
    {
        [record setSaleProduct:textField.text];
    }
    else if (textField.tag==103)
    {
        [record setCostProduct:textField.text];
    }



    [self addUpdateProducts:record recordNumber:indexPathRow];



    [self animateTextField:textField up:NO];


}

person Qaiser Abbas    schedule 05.06.2013    source источник


Ответы (3)


Этот проект делает что-то похожее на то, что вы пытаетесь сделать. Я рекомендую использовать это как пример.

person Stunner    schedule 05.06.2013

Вам нужен делегат текстового поля:

@interface ViewController () <UITextFieldDelegate> 

тогда попробуйте это:

@implementation ViewController

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

if (textField == self.myTextField) {

    [self.myTextField resignFirstResponder];

}

return YES;

}

person IlNero    schedule 05.06.2013

Я нашел решение этой проблемы

это было вызвано перезагрузкой данных.

для этого я пытаюсь перезагружать данные только тогда, когда клавиатура скрыта.

в viewDidLoad

 [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardDidShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];

затем используйте следующие методы

 -(void)reloadData
    {
        [_nextAppointmentTableView reloadData];
    //    [_nextAppointmentTableView reloadData];
        [self loadData];
    }


    - (void)keyboardDidShow:(NSNotification *)notification
    {

    }


    - (void)keyboardWillHide:(NSNotification *)notification
    {
        [self performSelector:@selector(reloadData) withObject:nil afterDelay:0.5];

    }
person Qaiser Abbas    schedule 14.06.2013